https://github.com/hexlab/bencode
Bencode is the encoding used by the p2p file sharing system BitTorrent
https://github.com/hexlab/bencode
Last synced: about 2 months ago
JSON representation
Bencode is the encoding used by the p2p file sharing system BitTorrent
- Host: GitHub
- URL: https://github.com/hexlab/bencode
- Owner: hexlab
- License: mit
- Created: 2019-12-07T10:27:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-18T15:30:30.000Z (over 5 years ago)
- Last Synced: 2024-07-31T21:53:12.232Z (9 months ago)
- Language: Python
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Bencode
Bencode is the encoding used by the p2p file sharing system BitTorrent.
## Example
```python
from bencode import bencode, bdecode
with open('1.torrent', 'rb') as binary_file:
data = binary_file.read()
res = bdecode(data)
bytes_ = bencode(res)
```## How to run tests
```python tests.py```
## Specs
#### Byte strings
Encoded as follows:
:
Examples:
3:foo represents the string "foo"
0: represents the empty string ""#### Integers
Integers are encoded as follows:
ie
Examples:
i3e represents the integer "3"
i-3e represents the integer "-3"
i-0e is invalid.
i03e are invalid, but
i0e represents the integer "0".#### Lists
Lists are encoded as follows:
le
The initial **l** and trailing **e** are beginning and ending delimiters.
Lists may contain any bencoded type, including integers, strings,
dictionaries, and even lists within other lists.Represents
l3:foo3:bare represents the list of two strings: ["foo", "bar"]
le represents an empty list: []#### Dictionaries
Dictionaries are encoded as follows:
de
The initial **d** and trailing **e** are the beginning and ending delimiters.
Note that the keys must be bencoded strings. The values may be any
bencoded type, including integers, strings, lists, and other
dictionaries. Keys must be strings and appear in sorted order
(sorted as raw strings, not alphanumerics). The strings should be
compared using a binary comparison, not a culture-specific "natural"
comparison.Examples:
d4:dead4:beef3:foo3:bare represents the dictionary {"dead": "beef", "foo": "bar"}
d3:fool1:a1:bee represents the dictionary {"foo": ["a", "b"]}
d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee represents {"publisher": "bob", "publisher-webpage": "www.example.com", "publisher.location": "home"}
de represents an empty dictionary {}