python 3中bencode编码的实现
torrent-bencode的Python项目详细描述
python 3中bencode编码的实现
用法
importbencodebencode.encode(OrderedDict((1,2),(b'eggs',b'spam')))bencode.decode(b'i3e')
编码的类型映射:
Bencode type | Python type |
---|---|
Dictionary | OrderedDict |
List | list |
Byte string | bytes |
Integer | int |
解码的类型映射:
Python type | Bencode type |
---|---|
dict | Dictionary |
list or tuple | List |
bytes | Byte string |
int | Integer |
如果传递的是ordereddict而不是普通dict,则订单将被保留。
压缩对等编码
该软件包还包括BEP 0023规定的“压缩”对等编码功能。虽然不是bencode本身的一部分,但它经常使用紧凑的对等编码。干线dht和大多数跟踪器以这种格式编码对等信息。
用法:
frombencode.miscimportpack_compact_peers_list,unpack_compact_peers_listpack_compact_peers_list((('127.0.0.1',8080),('127.0.0.1',9090)))b'\x7f\x00\x00\x01\x1f\x90\x7f\x00\x00\x01#\x82'unpack_compact_peers_list(b'\x7f\x00\x00\x01\x1f\x90\x7f\x00\x00\x01#\x82')(('127.0.0.1',8080),('127.0.0.1',9090))