Skip to main content

YABEncode - Yet Another Bencode module

Project description

An implementation of bencoding/bdecoding in Python 3, with somewhat descriptive Exceptions for decode errors. Also includes a command-line tool for decoding and pretty-printing bencoded data!


To install from PyPI:

pip install yabencode


In Python…

import yabencode
# bencode supports dicts, lists, ints and strings (bytestrings)
yabencode.encode({'foo':'baz', 'list':['eggs', 'spam', 'bacon']})
# Input can be string, bytes or a file object

    # Malformed data, 'spam' is missing an 'a'
except yabencode.MalformedBencodeException as e:
    # Unexpected data type (b':') at position 31 (0x1F hex)

    # Bencode does not support floats
except yabencode.BencodeException as e:
    # Unsupported type <class 'float'>

or with the command-line tool:

$ 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'',
 'announce-list': [[b''],
 'comment': b'Ubuntu CD',
 '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 '' | 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'}}}

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yabencode-0.2.1.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

yabencode-0.2.1-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page