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.
pip install bencode-pyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to encode Python dictionaries, integers, and lists into bencode format, and then decode bencoded bytes back into Python data structures. It also shows how to configure `bencodepy` to decode strings as UTF-8 rather than raw bytes.
Update `import bencode` statements to `import bencodepy`. For existing code relying on specific `bencode` module behavior, consider migrating to `bencodepy.Bencode` instances for explicit control.
Upgrade your Python environment to Python 3.6 or newer. Python 3.8+ is recommended for optimal compatibility.
When decoding, if `str` objects are desired, create a `Bencode` instance: `bc = bencodepy.Bencode(encoding='utf-8')` and use `bc.decode(bencoded_data)`. Remember to encode Python `str` to `bytes` (e.g., `s.encode('utf-8')`) before passing them to `bencodepy.encode`.If dictionary key order is critical, initialize `Bencode` with `dict_ordered=True`: `bc = bencodepy.Bencode(dict_ordered=True)`. This will cause decoded dictionaries to be `OrderedDict` instances. For advanced sorting, `dict_ordered_sort=True` can also be used.
After `import bencodepy`, use `bencodepy.decode(data)` or `bencodepy.encode(data)`. Do not call `decode(data)` or `encode(data)` directly.
Verify 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.
Before 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.No dependency data recorded yet.