Library for encoding and decoding bencode data.
Bencode (pronounced like B-encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data. It supports four different types of values:
- byte strings,
- integers,
- lists,
- dictionaries (associative arrays).
Bencoding is most commonly used in torrent files. These metadata files are simply bencoded dictionaries.
Description from wikipedia.
pip install bendercoder
For decoding and encoding the integer number 10.
# coding=utf-8
from decoder import Decoder
from encoder import Encoder
decoder = Decoder(payload=b'i10e')
decoded_value = decoder.decode()
print(decoded_value)
encoder = Encoder(payload=decoded_value)
encoded_value = encoder.encode()
print(encoded_value)
For decoding and encoding the string foo.
# coding=utf-8
from decoder import Decoder
from encoder import Encoder
decoder = Decoder(payload=b'3:foo')
decoded_value = decoder.decode()
print(decoded_value)
encoder = Encoder(payload=decoded_value)
encoded_value = encoder.encode()
print(encoded_value)
For decoding and encoding the list [1, 2, 3, 'bar']
# coding=utf-8
from decoder import Decoder
from encoder import Encoder
decoder = Decoder(payload=b'li1ei2ei3e3:bare')
decoded_value = decoder.decode()
print(decoded_value)
encoder = Encoder(payload=decoded_value)
encoded_value = encoder.encode()
print(encoded_value)
For decoding and encoding the dictionary {'test': 'foo', 'bar': 2}
# coding=utf-8
from decoder import Decoder
from encoder import Encoder
decoder = Decoder(payload=b'd4:test3:foo3:bari2ee')
decoded_value = decoder.decode()
print(decoded_value)
encoder = Encoder(payload=decoded_value)
encoded_value = encoder.encode()
print(encoded_value)