Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Python 3 bencoding implementation

Project description

Python 3 bencoding library

Build Status

Python 3 bytearray bencoding library.

Encode all data into byte. Useful for networking.


>>> from bencode import bencode, bdecode

>>> foo = bencode({"foo": [0, -1, 2, "3", {"4": 5}], "bar": {"baz": 1}})
>>> print(foo)

>>> bar = bdecode(foo)
>>> print(bar)
{b'bar': {b'baz': 1}, b'foo': [0, -1, 2, b'3', {b'4': 5}]}

decoder for bdecode

Optional argument in bdecode function, provide decode bytes to another types. It can be helpful for decode dict key into strings.

decoder is a callback function with arguments: field_type, value

  • field_type str, possible values: "key", "value";
  • value bytes.
>>> def custom_decoder(field_type, value):
...     if field_type == "key":
...         return str(value, "ascii")
...     elif field_type == "value":
...         return str(value, "utf-8")
...     else:
...         raise Exception("'field_type' can pass only 'key' and 'value' values")

>>> bar = bdecode(foo, decoder=custom_decoder)
>>> print(bar)
{'bar': {'baz': 1}, 'foo': [0, -1, 2, '3', {'4': 5}]}

Project details

Download files

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

Files for py3-bencode, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size py3_bencode-0.0.3-py3-none-any.whl (15.3 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size py3-bencode-0.0.3.tar.gz (2.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page