A python module for decoding bencoded data
Bencoding
Bencoding is a meta data representation format fot the BitTorrent Protocol (BTP). The Augmented BNF syntax for Bencoding is given below
dictionary = "d" 1*(string anytype) "e"
list = "l" 1*anytype "e"
integer = "i" signumber "e"
string = number ":" <number long sequence of any CHAR>
anytype = dictionary / list / integer / string
signumber = "-" number / number
number = 1*DIGIT
CHAR = %x00-FF
DIGIT = "0" / "1" / "2" / "3" / "4" /
"5" / "6" / "7" / "8" / "9"
Source: BTP RFC
Usage
Decoding bencoded data is simple.
>>> import bendcode
>>> bendcode.decode('i123e')
123
>>> bendcode.decode('4:John')
'John'
>>> bendcode.decode('li234ei123ee')
[234, 123]
>>> bendcode.decode('d1:ai123e1:bi234ee')
{'a': 123, 'b': 234}
You can decode invidual types too !
>>> import bendcode
>>> bendcode.match_string('3:abc')
('abc', '')
>>> bendcode.match_int('i-123e')
(-123, '')
>>> bendcode.match_list('li123ee')
([123], '')
>>> bendcode.match_dict('d1:a1:bei123e')
({'a': 'b'}, 'i123e')
Note: The match_* series of functions return a tuple (first_possbile_match, remaining_unmatched_string)
Bendcode can encode too
>>> import bendcode
>>> bendcode.encode(123)
'i123e'
>>> bendcode.encode('John')
'4:John'
>>> bendcode.encode([123, 'bro'])
'li123e3:broe'
>>> bendcode.encode({'hello': 123})
'd5:helloi123ee'
>>> bendcode.encode(None)
''
You decide whether to raise an exception or not for any functions mentioned above by setting the fail_silently parameter to True or False
>>> import bendcode
>>> bendcode.match_string('abc')
(None, 'abc')
>>> bendcode.match_string('abc', fail_silently=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "bendcode\bendcode.py", line 39, in match_string
raise MalformedBencodeError('Failed to match string in ' + str(raw))
bendcode.exceptions.MalformedBencodeError: Failed to match string in abc
Tests
Run tests using the following code
>>> from bendcode import tests >>> tests.run_tests() .................................. ---------------------------------------- Ran 34 tests in 0.015s OK
Issues
If you find any bug, feel free to create an issue here.
License
BenDcode uses GNU v2 License. Read the terms of the license here.
Metadata
Release files for bendcode 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| bendcode-1.1.0.zip | 16.1 kB | Without provenance | Details |
Release files / bendcode-1.1.0.zip
| Download URL | bendcode-1.1.0.zip |
|---|---|
| Size | 16.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
989b54d7bf2883bcba956d8808aaffe49b976bf82ea48598ed14927b3cfe5fe7
|
|
BLAKE2b-256 checksum How to use checksums |
08801bb338770a52f2f3d621c319671edda99e27401052606654ae7c202df0ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for bendcode 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.