API Reference¶
This module implements encoding and decoding of bittorrent’s bencoding.
-
pybencoding.
decode
(data)[source]¶ Decode a bencoded bytes object
Parameters: data (bytes) – A valid bencoded representation of some object. Returns: The decoded object. Return type: bytes, int, dict, list Raises: DecodeError
-
pybencoding.
encode
(val)[source]¶ Encode a value as a bytearray
Parameters: val – The value that is to be encoded. Can be of type
dict
,list
,int
,string
,bytes
. Dicts and lists can only contain those types. Note thatdict
objects may not contain keys with a type other thanbytes
orstring
and that all keys must have a uniquebytes
encoding. The following is not valid:{'hello': 1, b'hello': 2}
because:
'hello'.encode() == b'hello'
Besides the aforementioned types, any type that derives from them is also supported with the expectation that they behave in a sane way. If you supply derived types that don’t behave as we expect these types to behave the results are undefined.
Returns: The bencoded representation of val
Return type: bytes Raises: EncodeError