Skip to content
/ bencode Public

A Python module for encoding and decoding Bencode data

License

Notifications You must be signed in to change notification settings

gazev/bencode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bencode

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

Installation

Install it using pip.

pip install bencode-vug

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)

About

A Python module for encoding and decoding Bencode data

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages