Git Product home page Git Product logo

bencoder's Introduction

bencoder

Build Status

A simple up-to-date bencode decoder-encoder library on pure Python.

I wrote this simple implementation because of the lack of bencoding libraries compatible with Python 3.

Bencode (pronounced like B encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.

Installation

You can install the package directly from PyPI using pip.

pip install bencoder

Or you can use this source

git clone [email protected]:utdemir/bencoder.git
cd bencoder 
python setup.py install

Usage

>>> import bencoder

>>> print(bencoder.decode.__doc__)

    Decodes given bencoded bytes object.

    >>> decode(b'i-42e')
    -42
    >>> decode(b'4:utku') == b'utku'
    True
    >>> decode(b'li1eli2eli3eeee')
    [1, [2, [3]]]
    >>> decode(b'd3:bar4:spam3:fooi42ee') == {b'bar': b'spam', b'foo': 42}
    True
    
>>> print(bencoder.encode.__doc__)

    bencodes given object. Given object should be a int,
    bytes, list or dict.

    >>> [encode(i) for i in (-2, 42, b"answer", b"")]             
               == [b'i-2e', b'i42e', b'6:answer', b'0:']
    True
    >>> encode([b'a', 42, [13, 14]]) == b'l1:ai42eli13ei14eee'
    True
    >>> encode({b'bar': b'spam', b'foo': 42, b'mess': [1, b'c']}) \
               == b'd3:bar4:spam3:fooi42e4:messli1e1:cee'
    True

Real Life Scenario

>>> import bencoder
>>> f = open("archlinux-2014.05.01-dual.iso.torrent", "rb")
>>> d = bencoder.decode(f.read())
>>> del d[b"info"][b"pieces"] # That's a long hash
>>> from pprint import pprint
>>> pprint(d)
{b'announce': b'http://tracker.archlinux.org:6969/announce',
b'comment': b'Arch Linux 2014.05.01 (www.archlinux.org)',
b'created by': b'mktorrent 1.0',
b'creation date': 1398921725,
b'info': {b'length': 565182464,
        b'name': b'archlinux-2014.05.01-dual.iso',
        b'piece length': 524288},
b'url-list': [b'http://mirror.aarnet.edu.au/pub/archlinux/iso/2014.05.01/',
            b'http://ftp.iinet.net.au/pub/archlinux/iso/2014.05.01/',
            ....
            b'http://mirror-fpt-telecom.fpt.net/archlinux/iso/2014.05.01/']}

bencoder's People

Contributors

utdemir avatar qnnnnez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.