Yabencode-又一个bencode模块

yabencode的Python项目详细描述


python 3中bencoding/bdecoding的一个实现,具有一定的描述性 解码错误的例外。 还包括一个命令行工具,用于解码和漂亮的打印工作台数据!

安装

PyPI安装:

pip install yabencode

用法:

在python中…

importyabencode# bencode supports dicts, lists, ints and strings (bytestrings)yabencode.encode({'foo':'baz','list':['eggs','spam','bacon']})# Input can be string, bytes or a file objectyabencode.decode(b'd3:foo3:baz4:listl4:eggs4:spam5:baconee')try:# Malformed data, 'spam' is missing an 'a'yabencode.decode(b'd3:foo3:baz4:listl4:eggs4:spm5:baconee')exceptyabencode.MalformedBencodeExceptionase:print(e)# Unexpected data type (b':') at position 31 (0x1F hex)try:# Bencode does not support floatsyabencode.encode({'float':3.14})exceptyabencode.BencodeExceptionase:print(e)# Unsupported type <class 'float'>

或者使用命令行工具:

$ yabencode -h
usage: yabencode [-h][-t KEY][-r] FILE

Bdecode a file/standard input and pretty-print the resulting data

positional arguments:
  FILE                  Input file. Use - for stdin

optional arguments:
  -h, --help            show this help message and exit
  -t KEY, --truncate KEY
                        Truncate values under given key. May be repeated for
                        multiple values
  -r, --raw             Raw keys - do not decode dictionary keys

$ # The 'pieces'-bytestring is rather long, so let's truncate it
$ yabencode -t pieces ubuntu-17.04-desktop-amd64.iso.torrent
{'announce': b'http://torrent.ubuntu.com:6969/announce',
 'announce-list': [[b'http://torrent.ubuntu.com:6969/announce'],
                   [b'http://ipv6.torrent.ubuntu.com:6969/announce']],
 'comment': b'Ubuntu CD releases.ubuntu.com',
 'creation date': 1492077159,
 'info': {'length': 1609039872,
          'name': b'ubuntu-17.04-desktop-amd64.iso',
          'piece length': 524288,
          'pieces': '<truncated>'}}

$ # Reading bytes from stdin (using -r to not decode the keys)
$ curl -s 'http://torrent.ubuntu.com:6969/scrape?info_hash=%59%06%67%69%b9%ad%42%da%2e%50%86%11%c3%3d%7c%44%80%b3%85%7b'| yabencode -r -
{b'files': {b'Y\x06gi\xb9\xadB\xda.P\x86\x11\xc3=|D\x80\xb3\x85{': {b'complete': 3473,
                                                                    b'downloaded': 33029,
                                                                    b'incomplete': 102,
                                                                    b'name': b'ubuntu-17.04-desktop-amd64.iso'}}}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaGWT:何时使用Lazydemelement?   Java中跟踪消失线程的多线程处理   java Springboot未能配置数据源:“url”,但我没有使用数据库   java为按钮生成随机位置   math Java:包含二项式系数计算的表达式   java通过AsyncTask传递参数   从路径错误创建java文件   高流量网站的性能播放框架、Java、Apache、PostgreSQL、JPA和Hibernate   java将4D矢量转换为长矢量   arraylist Java循环在没有任何错误的情况下终止   java正在制作一个计算器应用程序,希望在第二个片段中更新历史,但无法完成   java将信息从IntentService发送到Activity   java如何在游戏中插入大量实体!工作   javascript如何在ScriptEngineforJava中从数学中获得准确的结果?