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!

Installing

To install from PyPI:

pip install yabencode

Usage:

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
yabencode.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')
except yabencode.MalformedBencodeException as e:
    print(e)
    # Unexpected data type (b':') at position 31 (0x1F hex)

try:
    # Bencode does not support floats
    yabencode.encode({'float':3.14})
except yabencode.BencodeException as e:
    print(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'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'}}}

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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file yabencode-0.2.1.tar.gz.

File metadata

  • Download URL: yabencode-0.2.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.3

File hashes

Hashes for yabencode-0.2.1.tar.gz
Algorithm Hash digest
SHA256 09ff3171b97bb73d047d7191f91198f8d570f6c3726219ca2b0bb2c926fed04c
MD5 6153732fe8b05e61e4c97fe319443bf9
BLAKE2b-256 07ebb0fc95d4213859a87ad03cd4b7c3fa44c3a49a715cdd953a56c7438d7f92

See more details on using hashes here.

File details

Details for the file yabencode-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: yabencode-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.3

File hashes

Hashes for yabencode-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4183cb07551ead1ea139ca22b9fe7ccf2a71f0fe6eedbec0422a540db68928e
MD5 5a55cb2c7cf941a26fca219a4d9c689c
BLAKE2b-256 672073f6f250f36aeb316b328fa2204a59d7d3440c3de8c07e72eeadfaa5a983

See more details on using hashes here.

Supported by

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