https://github.com/bashkirtsevich-llc/py3bencode
Python 3.7 bencoding library
https://github.com/bashkirtsevich-llc/py3bencode
bencode bencode-parser bittorrent
Last synced: 6 months ago
JSON representation
Python 3.7 bencoding library
- Host: GitHub
- URL: https://github.com/bashkirtsevich-llc/py3bencode
- Owner: bashkirtsevich-llc
- License: gpl-3.0
- Created: 2017-09-16T17:11:15.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-04T10:41:07.000Z (almost 6 years ago)
- Last Synced: 2024-10-05T06:55:35.224Z (8 months ago)
- Topics: bencode, bencode-parser, bittorrent
- Language: Python
- Homepage: https://pypi.org/project/py3-bencode/
- Size: 32.2 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Python 3 bencoding library
[](https://travis-ci.org/bashkirtsevich-llc/py3bencode)Python 3 bytearray bencoding library.
Encode all data into `byte`. Useful for networking.
## Example
```python
>>> from bencode import bencode, bdecode>>> foo = bencode({"foo": [0, -1, 2, "3", {"4": 5}], "bar": {"baz": 1}})
>>> print(foo)
b'd3:bard3:bazi1ee3:fooli0ei-1ei2e1:3d1:4i5eeee'>>> bar = bdecode(foo)
>>> print(bar)
{b'bar': {b'baz': 1}, b'foo': [0, -1, 2, b'3', {b'4': 5}]}```
## `decoder` for `bdecode`
Optional argument in `bdecode` function, provide decode bytes to another types. It can be helpful for decode dict key into strings.
`decoder` is a callback function with arguments: `field_type`, `value`
* `field_type` **str**, possible values: `"key"`, `"value"`;
* `value` **bytes**.```python
>>> def custom_decoder(field_type, value):
... if field_type == "key":
... return str(value, "ascii")
... elif field_type == "value":
... return str(value, "utf-8")
... else:
... raise Exception("'field_type' can pass only 'key' and 'value' values")
...>>> bar = bdecode(foo, decoder=custom_decoder)
>>> print(bar)
{'bar': {'baz': 1}, 'foo': [0, -1, 2, '3', {'4': 5}]}
```