Skip to main content

bencode.py

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)

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

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)

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

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.0.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.0.0
File Size Uploaded Provenance Actions
bencode.py-4.0.0.tar.gz 19.8 kB Without provenance Details for bencode.py-4.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bencode.py 4.0.0
File Interpreter ABI Platform Actions
bencode.py-4.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for bencode.py-4.0.0-py2.py3-none-any.whl

Total release size: 38.8 kB

Release files / bencode.py-4.0.0.tar.gz

Download URL bencode.py-4.0.0.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2a24ccda1725a51a650893d0b63260138359eaa299bb6e7a09961350a2a6e05c
BLAKE2b-256 checksum
How to use checksums
e86f1fc1f714edc73a9a42af816da2bda82bbcadf1d7f6e6cae854e7087f579b
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/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.15

Release files / bencode.py-4.0.0-py2.py3-none-any.whl

Download URL bencode.py-4.0.0-py2.py3-none-any.whl
Size 19.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
99c06a55764e85ffe81622fdf9ee78bd737bad3ea61d119784a54bb28860d962
BLAKE2b-256 checksum
How to use checksums
159feabbc8c8a16db698d9c4bd24953763df2594b054237b89afe1ec56d3965e
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/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.15

Security for bencode.py 4.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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