Skip to main content

Bencode encoder/decoder written in Python 3 under the GPLv2.

Project description

#BencodePy
A small Python 3 library for encoding and decoding Bencode data licensed under the GPLv2.

##Overview
Although Bencoding is mainly, if not exclusively, used for BitTorrent metadata (.torrent) files, this library seeks to
provide a generic means of encoding/decoding Bencode from/to Python data structures independent of torrent files.

##Docs

### Installation
`pip install bencodepy`

### Encode

```python
from bencodepy import encode
mydata = { 'keyA': 'valueA' } #example data
bencoded_data = encode(mydata)
print (bencoded_data)
>>> b'd4:keyA6:valueAe'
```

###Mappings

Python Type* | Bencode Type
------------- | -------------
dict | Dictionary
list | List
tuple | List
int | Integer
str | String
bytes | String

*Includes subtypes thus both dict and OrderedDict would be represented as Bencode dictionary.

### Decode

From bytes...
```python
from bencodepy import decode
mydata = b'd4:KeyA6:valueAe'
my_ordred_dict = decode(mydata)
print(my_ordred_dict)
>>> OrderedDict([(b'KeyA', b'valueA')])
```

Alternatively from a file...
```python
from bencodepy import decode_from_file
my_file_path = 'c:\whatever'
my_ordred_dict = decode_from_file(my_file_path)
```

###Mapping:

Bencode Type | Python Type
------------- | -------------
Dictionary | OrderedDict
List | list
Integer | int
String | bytes

Bencode dictionaries are decoded as Python OrderedDict to preserve the order of elements. This is necessary to correctly
calculate certain hash values such as that of a torrent file's Info Dictionary.

Decode methods will always return an iterable. If the root element of the bencode data is not a dictionary or
list, `decode()` will wrap the all bencode elements in a tuple. Thus input data of `b'5:ItemA5:ItemB'` would yield
a python tuple of `('ItemA', 'ItemB')`.


##TODO
1. Benchmarks for encoding.
2. Determine method of distributing the optimized (cythonized) version of bencodepy.

##License
Copyright © 2014 by Eric Weast

Licensed under the [GPLv2](https://www.gnu.org/licenses/gpl-2.0.html "gnu.org")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bencodepy-0.9.4.zip (4.3 kB view details)

Uploaded Source

File details

Details for the file bencodepy-0.9.4.zip.

File metadata

  • Download URL: bencodepy-0.9.4.zip
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bencodepy-0.9.4.zip
Algorithm Hash digest
SHA256 7405ebef78bf0d4e8d477f792c2b1fdc7e830b7a2884c41bb4c43974ca1d1cf7
MD5 9f769b69deb7ae1b38e448e52b9598f6
BLAKE2b-256 5251dd6a8a37533848d5a5a5996993522b17ceb57b0a66ba6a9d78012d8da8e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page