Bencode.py
Bencode.py is a simple bencode parser for Python, compatible with Python 2, Python 3, and PyPy. Version 4.0.0 is the current stable release, which refactored the API into a new module, `bencodepy`. The library provides an intuitive API for encoding and decoding Bencode data, a serialization format primarily used in BitTorrent, and maintains an active release cadence.
Common errors
-
NameError: name 'bdecode' is not defined
cause The functions for encoding and decoding are methods of the `bencodepy` module (or `bencode` for legacy) directly, not standalone global functions.fixAfter `import bencodepy`, use `bencodepy.decode(data)` or `bencodepy.encode(data)`. Do not call `decode(data)` or `encode(data)` directly. -
bencodepy.exceptions.BencodeDecodeError: not a valid bencoded string
cause The input byte string provided to `decode` does not conform to the Bencode specification, indicating corruption, truncation, or incorrect data format.fixVerify the integrity and source of the bencoded string. Ensure it is complete and properly formatted Bencode. Debug by attempting to decode a known-good bencoded string to isolate the issue. -
TypeError: a bytes-like object is required, not 'str'
cause Python 3 distinguishes between `str` (Unicode) and `bytes`. Bencode operations require `bytes` objects for both input and output (unless an explicit `encoding` is set for decoding). Passing a `str` directly without encoding it will raise this error.fixBefore encoding, ensure all string values in your Python data are converted to `bytes` (e.g., `my_string.encode('utf-8')`). When decoding, if you require `str` objects, initialize `bencodepy.Bencode(encoding='utf-8')` to perform automatic decoding.
Warnings
- breaking Version 4.0.0 introduces a significant API change. The primary, improved API for encoding and decoding has moved from `bencode` to a new top-level module named `bencodepy`. While `import bencode` still works for backward compatibility, it's recommended to update imports to `import bencodepy` and use its functions directly.
- breaking Version 3.0.0 dropped official support for Python 2.6 and Python 3.4. Code running on these Python versions will not be supported and may encounter issues.
- gotcha Bencode strings are inherently byte sequences, not Unicode strings. By default, `bencodepy.decode` will return byte strings (`bytes`). If you expect Python `str` objects, you must explicitly specify an `encoding` (e.g., 'utf-8') when creating a `Bencode` instance for decoding.
- gotcha Bencode dictionaries do not guarantee key order. While `bencodepy` can decode to a standard Python `dict`, certain applications (e.g., BitTorrent info hash calculations) require `OrderedDict` to preserve key order.
Install
-
pip install bencode-py
Imports
- bencodepy
import bencode
import bencodepy
- Bencode
from bencodepy import Bencode
Quickstart
import bencodepy
# Encode Python data to bencode bytes
data_to_encode = {
'message': 'hello world',
'number': 123,
'list_of_items': ['item1', 2, b'binary_data']
}
bencoded_data = bencodepy.encode(data_to_encode)
print(f"Encoded: {bencoded_data}")
# Decode bencode bytes to Python data
decoded_data = bencodepy.decode(bencoded_data)
print(f"Decoded: {decoded_data}")
# Decode to UTF-8 strings by default using a custom Bencode instance
bc = bencodepy.Bencode(encoding='utf-8')
utf8_decoded = bc.decode(b'd7:message11:hello worlde')
print(f"UTF-8 Decoded: {utf8_decoded}')