如何解码本编码的torrent数据 - python

我正在尝试使用Bencode解码torrent文件的内容来从torrent文件中提取大小和名称。

我做了pip install bencode之后,我用torrent文件的其中一行进行了测试,如您所见。

import bencode

blabla = 'd8:announce70:http://tracker.t411.io:56969/c5faa6720249d33ff6ba2af48640af89/announce7:comment29:https://www.t411.io/t/524280210:created by19:https://www.t411.io13:creation datei1431685353e4:infod6:lengthi14634059e4:name22:Charlie-Hebdo-1178.pdf12:piece lengthi262144e6:pieces1120:'
myprint = bencode.decode_string(blabla,1)
print myprint

这是pip安装在python lib中的文件:

from BTL import BTFailure


def decode_int(x, f):
    f += 1
    newf = x.index('e', f)
    n = int(x[f:newf])
    if x[f] == '-':
        if x[f + 1] == '0':
            raise ValueError
    elif x[f] == '0' and newf != f+1:
        raise ValueError
    return (n, newf+1)

def decode_string(x, f):
    colon = x.index(':', f)
    n = int(x[f:colon])
    if x[f] == '0' and colon != f+1:
        raise ValueError
    colon += 1
    return (x[colon:colon+n], colon+n)

def decode_list(x, f):
    r, f = [], f+1
    while x[f] != 'e':
        v, f = decode_func[x[f]](x, f)
        r.append(v)
    return (r, f + 1)

def decode_dict(x, f):
    r, f = {}, f+1
    while x[f] != 'e':
        k, f = decode_string(x, f)
        r[k], f = decode_func[x[f]](x, f)
    return (r, f + 1)

decode_func = {}
decode_func['l'] = decode_list
decode_func['d'] = decode_dict
decode_func['i'] = decode_int
decode_func['0'] = decode_string
decode_func['1'] = decode_string
decode_func['2'] = decode_string
decode_func['3'] = decode_string
decode_func['4'] = decode_string
decode_func['5'] = decode_string
decode_func['6'] = decode_string
decode_func['7'] = decode_string
decode_func['8'] = decode_string
decode_func['9'] = decode_string

def bdecode(x):
    try:
        r, l = decode_func[x[0]](x, 0)
    except (IndexError, KeyError, ValueError):
        raise BTFailure("not a valid bencoded string")
    if l != len(x):
        raise BTFailure("invalid bencoded value (data after valid prefix)")
    return r

from types import StringType, IntType, LongType, DictType, ListType, TupleType


class Bencached(object):

    __slots__ = ['bencoded']

    def __init__(self, s):
        self.bencoded = s

def encode_bencached(x,r):
    r.append(x.bencoded)

def encode_int(x, r):
    r.extend(('i', str(x), 'e'))

def encode_bool(x, r):
    if x:
        encode_int(1, r)
    else:
        encode_int(0, r)

def encode_string(x, r):
    r.extend((str(len(x)), ':', x))

def encode_list(x, r):
    r.append('l')
    for i in x:
        encode_func[type(i)](i, r)
    r.append('e')

def encode_dict(x,r):
    r.append('d')
    ilist = x.items()
    ilist.sort()
    for k, v in ilist:
        r.extend((str(len(k)), ':', k))
        encode_func[type(v)](v, r)
    r.append('e')

encode_func = {}
encode_func[Bencached] = encode_bencached
encode_func[IntType] = encode_int
encode_func[LongType] = encode_int
encode_func[StringType] = encode_string
encode_func[ListType] = encode_list
encode_func[TupleType] = encode_list
encode_func[DictType] = encode_dict

try:
    from types import BooleanType
    encode_func[BooleanType] = encode_bool
except ImportError:
    pass

def bencode(x):
    r = []
    encode_func[type(x)](x, r)
    return ''.join(r)

事实是,我不太了解如何使用此Bencode解码行。

我已经尝试过def bdecode,但这是输出:

root@debian:/home/florian/Téléchargements# python decript.py 
Traceback (most recent call last):
  File "decript.py", line 4, in <module>
    myprint = bencode.bdecode(blabla)
  File "/usr/local/lib/python2.7/dist-packages/bencode/__init__.py", line 68, in bdecode
    raise BTFailure("not a valid bencoded string")
bencode.BTL.BTFailure: not a valid bencoded string

因此,我尝试使用def decode_string,但使用decode_string(blabla, 1)只能解码第一个单词:

root@debian:/home/florian/Téléchargements# python decript.py 
('announce', 11)

而数字2、3、4则无效,并显示如下错误:

root@debian:/home/florian/Téléchargements# python decript.py 
Traceback (most recent call last):
  File "decript.py", line 4, in <module>
    myprint = bencode.decode_string(blabla,10)
  File "/usr/local/lib/python2.7/dist-packages/bencode/__init__.py", line 29, in decode_string
    n = int(x[f:colon])
ValueError: invalid literal for int() with base 10: 'e70'

我想对所有行进行解码,但我不明白如何使用此bencode进行编码。

python大神给出的解决方案

您有不完整的Bencoded字符串。

第一部分告诉您有一本字典:

d...

应该解析它直到有一个e字符。您的输入字符串中没有这样的字符。

手动分析显示您具有键announcecommentcreated bycreation dateinfo,其中后者是带有lengthnamepiece-length和< cc>。然后,您的弦停止了; pieces没有值,也没有pieces标记外部词典或嵌套的e词典的末尾。我们所拥有的只是类型和长度指示符:info

您可以尝试直接使用解码功能,但要考虑到它们返回值和偏移量:

>>> bencode.decode_string(blabla, 1)
('announce', 11)

11是下一个值的偏移量:

>>> bencode.decode_string(blabla, 11)
('http://tracker.t411.io:56969/c5faa6720249d33ff6ba2af48640af89/announce', 84)

84再次是下一个:

>>> bencode.decode_string(blabla, 84)
('comment', 93)

如果考虑到字符串不完整并且并非所有编码对象都是字符串,则仍可以解​​码其中的内容。

偏移量还告诉您使用什么函数进行解码:

>>> blabla[1]
'8'
>>> bencode.decode_func[blabla[1]]
<function decode_string at 0x1004632a8>

这里的数字说明了预期的字符数。因此,跳过失败的1120词典映射,您将获得:

>>> offset = 1
>>> while True:
...     value, offset = bencode.decode_func[blabla[offset]](blabla, offset)
...     print value
... 
announce
http://tracker.t411.io:56969/c5faa6720249d33ff6ba2af48640af89/announce
comment
https://www.t411.io/t/5242802
created by
https://www.t411.io
creation date
1431685353
info
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/bencode/__init__.py", line 44, in decode_dict
    while x[f] != 'e':
IndexError: string index out of range

失败,因为您打了没有d的嵌套字典。您也可以通过在最后一个偏移量上添加一个来提取这些键:

>>> offset
194
>>> blabla[offset]
'd'
>>> offset += 1
>>> while True:
...     value, offset = bencode.decode_func[blabla[offset]](blabla, offset)
...     print value
... 
length
14634059
name
Charlie-Hebdo-1178.pdf
piece length
262144
pieces

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: string index out of range

或者,您可以将数据读取为二进制数据而不截断它:

with open(torrentfilename, 'rb') as torrentfile:
    torrent = bencode.bdecode(torrentfile.read())
# now you have a dictionary.