Skip to main content
https://img.shields.io/pypi/v/bencode.py.svg?style=flat-square https://img.shields.io/travis/fuzeman/bencode.py.svg?style=flat-square https://img.shields.io/coveralls/fuzeman/bencode.py/master.svg?style=flat-square

Simple bencode parser (for Python 2, Python 3 and PyPy), forked from the bencode package by Thomas Rampelberg.

Usage

Encode:

>>> import bencodepy

>>> bencodepy.encode({'title': 'Example'})
b'd5:title7:Examplee'

>>> bencodepy.encode(12)
b'i12e'

Decode:

>>> import bencodepy

>>> bencodepy.decode('d5:title7:Examplee')
{b'title': b'Example'}

>>> bencodepy.decode('i12e')
12

Decode to UTF-8:

>>> import bencodepy

>>> bc = bencodepy.Bencode(
    encoding='utf-8'
)

>>> bc.decode('d5:title7:Examplee')
{'title': 'Example'}

bencode

(legacy, backwards-compatible package)

This package will continue to be provided for backwards-compatibility, but upgrading to bencodepy is recommended for more reliable decoding results.

Under-the-hood this just provides proxies to a Bencode instance created with:

Bencode(
    encoding='utf-8',
    encoding_fallback='value',
    dict_ordered=True,
    dict_ordered_sort=True
)

Encode:

>>> import bencode

>>> bencode.encode({'title': 'Example'})
'd5:title7:Examplee'

>>> bencode.encode(12)
'i12e'

Decode:

>>> import bencode

>>> bencode.decode('d5:title7:Examplee')
OrderedDict([(u'title', u'Example')])

>>> bencode.decode('i12e')
12

API

bencodepy.Bencode(encoding=None, encoding_fallback=None, dict_ordered=False, dict_ordered_sort=False, max_depth=DEFAULT_MAX_DEPTH)

Create instance

  • encoding

    Encoding to decode strings with (or None for binary)

  • encoding_fallback

    Fallback to binary when decoding fails on the specified string types.

    • key - dictionary keys

    • value - values

    • all - always fallback to binary

    • None - always raise decoding errors

  • dict_ordered

    Use OrderedDict

  • dict_ordered_sort

    Ensure OrderedDict is sorted

  • max_depth

    Maximum structure depth before raising a BencodeDecodeError.

Methods:

  • decode(value)

    Decode bencode string value.

  • encode(value)

    Encode value into a bencode string.

  • read(fd)

    Decode bencode from file or path fd.

  • write(data, fd)

    Encode data to file or path fd.

bencodepy.BencodeDecoder(encoding=None, encoding_fallback=None, dict_ordered=False, dict_ordered_sort=False, max_depth=DEFAULT_MAX_DEPTH)

Create decoder

  • encoding

    Encoding to decode strings with (or None for binary)

  • encoding_fallback

    Fallback to binary when decoding fails on the specified string types.

    • key - dictionary keys

    • value - values

    • all - always fallback to binary

    • None - always raise decoding errors

  • dict_ordered

    Use OrderedDict

  • dict_ordered_sort

    Ensure OrderedDict is sorted

  • max_depth

    Maximum structure depth before raising a BencodeDecodeError.

Methods:

  • decode(value)

    Decode bencode string value.

bencodepy.BencodeEncoder()

Create encoder

Methods:

  • encode(value)

    Encode value into a bencode string.

bencodepy.bencode(value)

bencodepy.encode(value)

Encode value into a bencode string with the default encoder.

bencodepy.bdecode(value)

bencodepy.decode(value)

Decode bencode string value with the default decoder.

bencodepy.bread(fd)

Decode bencode from file or path fd with the default decoder.

bencodepy.bwrite(data, fd)

Encode data to file or path fd with the default encoder.

bencode

bencode.bencode(value)

bencode.encode(value)

Encode value into the bencode format.

bencode.bdecode(value)

bencode.decode(value)

Decode bencode formatted string value.

bencode.bread(fd)

Read bencode formatted string from file or path fd.

bencode.bwrite(data, fd)

Write data as a bencode formatted string to file or path fd.

Metadata

Release files for bencode.py 4.1.0

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

Source distribution (sdist)

Source distribution for bencode.py 4.1.0
File Size Uploaded
bencode_py-4.1.0.tar.gz 15.0 kB Details

Built distribution (wheel)

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

Total release size: 33.7 kB

Release files / bencode_py-4.1.0.tar.gz

Download URL bencode_py-4.1.0.tar.gz
Size 15.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f93372b45dc370fe4024f9230128f1f9fbc0fe30c3d41f1c3a7d02fe6c899813
BLAKE2b-256 checksum
How to use checksums
c5e997a84f30337c38f1572baa299eaeacc3ca090c6d383da9839edad166fef4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / bencode_py-4.1.0-py3-none-any.whl

Download URL bencode_py-4.1.0-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eec5d11eda36f008351cafa5d8ee51e2e14c9f2aa34df8e31f40e73ae78235a4
BLAKE2b-256 checksum
How to use checksums
5efa34e8fc47e05801820f3f2ef13c8c9b873e6268c9b69ca7f69335f3300ef6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

4.1.0 This release

2 release files

4.0.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

0.0.0

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