Skip to main content

Python 3 bencoding library

Build Status

Python 3 bytearray bencoding library.

Encode all data into byte. Useful for networking.

Example

>>> from bencode import bencode, bdecode


>>> foo = bencode({"foo": [0, -1, 2, "3", {"4": 5}], "bar": {"baz": 1}})
>>> print(foo)
b'd3:bard3:bazi1ee3:fooli0ei-1ei2e1:3d1:4i5eeee'

>>> 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}]}

Metadata

Release files for py3-bencode 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py3-bencode 0.0.3
File Size Uploaded
py3-bencode-0.0.3.tar.gz 2.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py3-bencode 0.0.3
File Interpreter ABI Platform
py3_bencode-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / py3-bencode-0.0.3.tar.gz

Download URL py3-bencode-0.0.3.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
154c08a9014c563df1495d41992c587e594de9a45ef745a9554035830541ea9b
BLAKE2b-256 checksum
How to use checksums
cbd9ffa41e93662879e1f1592df5244337dda783f4d871bbf6e531d611d65f7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1

Release files / py3_bencode-0.0.3-py3-none-any.whl

Download URL py3_bencode-0.0.3-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac451a07a712ebbeb5bf9b56f0dfbb9196dff86cdba1d519c478e8794d6f0ef2
BLAKE2b-256 checksum
How to use checksums
e7dda1fa118566f27359cf4b38fbe0bcb865427379047e254a1bafc751e75b1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page