A fast and correct bencode serialize/deserialize library
Actively maintained. Last release 7 weeks ago, commits in 20 of the last 26 weeks. A safe choice.
pip install bencode2A fast and correct bencode serialize/deserialize library
introduction
Why yet another bencode package in python?
because I need a bencode library:
1. Correct
It should fully validate its inputs, both encoded bencode bytes, or python object to be encoded.
And it should not decode bencode bytes to str by default.
Bencode doesn't have a utf-8 str type, only bytes, so many decoder try to decode bytes to str and fallback to bytes, this package won't, it parse bencode bytes value as python bytes.
It may be attempting to parse all dictionary keys as string,
but for BitTorrent v2 torrent, the keys in pieces root dictionary is still sha256 hash
instead of ascii/utf-8 string.
If you prefer string as dictionary keys, write a dedicated function to convert parsing result.
Also be careful! Even file name or torrent name may not be valid utf-8 string.
2. Fast enough
this package is written with c++ in CPython.
3. still cross implement
This package sill have a pure python wheel bencode2-${version}-py3-none-any.whl wheel
on pypi.
Which means you can still use it in non-cpython python with same behavior.
install
pypi
pip install bencode2
conda/pixi
you can install conda package bencode2 from https://prefix.dev/channels/trim21-pkgs/packages/bencode2.
basic usage
import bencode2
assert bencode2.bdecode(b"d4:spaml1:a1:bee") == {b"spam": [b"a", b"b"]}
assert bencode2.bencode({'hello': 'world'}) == b'd5:hello5:worlde'