Skip to main content

A package to encode and decode bencoded data

Project description

Bencode

A module that mimics the json standard lib module interface for Bencode

Installation

Simply clone the repository and then run the following on the root dir of the repo.

python3 -m build && python3 -m pip install dist/bencode-0.0.1-py3-none-any.whl

Usage

Reading bencoded data

Both load and loads functions will decode bencoded data.

import bencode

decoded_dic = bencode.loads(encoded_data)

# deserializing bencoded data from a file
with open("bencoded_file.torrent", "rb") as fp:
    decoded_dic = bencode.load(fp)

Encoding data

Pragmatically, only dictionaries are encoded in real life applications of this encoding format, but any other supported types can be singularly encoded.

import bencode

peers_dic = {b'peers': [
                {b'127.0.0.1': b'29123'}, 
                {b'127.0.0.1': b'6432'}
            ]}

# b'd5:peersld9:127.0.0.15:29123ed9:127.0.0.14:6432eee'
bencoded_peers = bencode.dumps(peers_dic)

# serialize an object as a bencoded stream to a file:
with open("my_bencoded_file.torrent", "rb") as fp:
    bencode.dump(peers_dic, fp)

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

bencode_vug-0.1.0.tar.gz (40.7 kB view hashes)

Uploaded Source

Built Distribution

bencode_vug-0.1.0-py3-none-any.whl (28.8 kB view hashes)

Uploaded Python 3

Supported by

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