├── .travis.yml ├── .gitignore ├── setup.py ├── README.md ├── bencoder.py └── LICENSE.txt /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - '2.7' 4 | - '3.2' 5 | - '3.3' 6 | - '3.4' 7 | - pypy 8 | script: nosetests --with-doctest bencoder.py 9 | deploy: 10 | provider: pypi 11 | user: utdemir 12 | password: 13 | secure: MwI5xm7iOj6LTk47yUTyUGWdgOCAt7DQffj/vwq+7LCjJPphHp3IPZJwQvtru0bVlOv163Lf1vxBVV0b8f/18sMPr07JvlpWfx3enQDTXcmCVVtyGiZSSN8/H/TSZX36xnZ3VG2swOUXx9KX33F7e5MOn1LP5XPCvzePd4QM0tQ= 14 | on: 15 | tags: true 16 | repo: utdemir/bencoder 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by http://www.gitignore.io 2 | 3 | ### Python ### 4 | # Byte-compiled / optimized / DLL files 5 | __pycache__/ 6 | *.py[cod] 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | env/ 14 | ENV/ 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # Installer logs 29 | pip-log.txt 30 | pip-delete-this-directory.txt 31 | 32 | # Unit test / coverage reports 33 | htmlcov/ 34 | .tox/ 35 | .coverage 36 | .cache 37 | nosetests.xml 38 | coverage.xml 39 | 40 | # Translations 41 | *.mo 42 | *.pot 43 | 44 | # Django stuff: 45 | *.log 46 | 47 | # Sphinx documentation 48 | docs/_build/ 49 | 50 | 51 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from distutils.core import setup 4 | 5 | setup(name='bencoder', 6 | version='0.2.1', 7 | description='A simple bencode decoder/encoder library in pure Python.', 8 | url='https://github.com/utdemir/bencoder', 9 | author='Utku Demir', 10 | author_email='utdemir@gmail.com', 11 | py_modules=['bencoder'], 12 | classifiers=[ 13 | 'Environment :: Other Environment', 14 | 'Intended Audience :: Developers', 15 | 'License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication', 16 | 'Operating System :: OS Independent', 17 | 'Programming Language :: Python :: 2', 18 | 'Programming Language :: Python :: 3', 19 | 'Topic :: Software Development :: Libraries :: Python Modules' 20 | ], 21 | ) 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bencoder 2 | 3 | [![Build Status](https://travis-ci.org/utdemir/bencoder.svg?branch=master)](https://travis-ci.org/utdemir/bencoder) 4 | 5 | A simple up-to-date bencode decoder-encoder library on pure Python. 6 | 7 | I wrote this simple implementation because of the lack of bencoding libraries compatible with Python 3. 8 | 9 | *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.* 10 | 11 | ## Installation 12 | 13 | You can install the package directly from PyPI using pip. 14 | 15 | pip install bencoder 16 | 17 | Or you can use this source 18 | 19 | git clone git@github.com:utdemir/bencoder.git 20 | cd bencoder 21 | python setup.py install 22 | 23 | ## Usage 24 | 25 | >>> import bencoder 26 | 27 | >>> print(bencoder.decode.__doc__) 28 | 29 | Decodes given bencoded bytes object. 30 | 31 | >>> decode(b'i-42e') 32 | -42 33 | >>> decode(b'4:utku') == b'utku' 34 | True 35 | >>> decode(b'li1eli2eli3eeee') 36 | [1, [2, [3]]] 37 | >>> decode(b'd3:bar4:spam3:fooi42ee') == {b'bar': b'spam', b'foo': 42} 38 | True 39 | 40 | >>> print(bencoder.encode.__doc__) 41 | 42 | bencodes given object. Given object should be a int, 43 | bytes, list or dict. 44 | 45 | >>> [encode(i) for i in (-2, 42, b"answer", b"")] 46 | == [b'i-2e', b'i42e', b'6:answer', b'0:'] 47 | True 48 | >>> encode([b'a', 42, [13, 14]]) == b'l1:ai42eli13ei14eee' 49 | True 50 | >>> encode({b'bar': b'spam', b'foo': 42, b'mess': [1, b'c']}) \ 51 | == b'd3:bar4:spam3:fooi42e4:messli1e1:cee' 52 | True 53 | 54 | ### Real Life Scenario 55 | 56 | >>> import bencoder 57 | >>> f = open("archlinux-2014.05.01-dual.iso.torrent", "rb") 58 | >>> d = bencoder.decode(f.read()) 59 | >>> del d[b"info"][b"pieces"] # That's a long hash 60 | >>> from pprint import pprint 61 | >>> pprint(d) 62 | {b'announce': b'http://tracker.archlinux.org:6969/announce', 63 | b'comment': b'Arch Linux 2014.05.01 (www.archlinux.org)', 64 | b'created by': b'mktorrent 1.0', 65 | b'creation date': 1398921725, 66 | b'info': {b'length': 565182464, 67 | b'name': b'archlinux-2014.05.01-dual.iso', 68 | b'piece length': 524288}, 69 | b'url-list': [b'http://mirror.aarnet.edu.au/pub/archlinux/iso/2014.05.01/', 70 | b'http://ftp.iinet.net.au/pub/archlinux/iso/2014.05.01/', 71 | .... 72 | b'http://mirror-fpt-telecom.fpt.net/archlinux/iso/2014.05.01/']} 73 | 74 | -------------------------------------------------------------------------------- /bencoder.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | A simple bencoding implementation in pure Python. 5 | 6 | Consult help(encode) and help(decode) for more info. 7 | 8 | >>> encode(42) == b'i42e' 9 | True 10 | >>> decode(b'i42e') 11 | 42 12 | """ 13 | 14 | import re 15 | import string 16 | import itertools as it 17 | 18 | def encode(obj): 19 | """ 20 | bencodes given object. Given object should be a int, 21 | bytes, list or dict. If a str is given, it'll be 22 | encoded as ASCII. 23 | 24 | >>> [encode(i) for i in (-2, 42, b"answer", b"")] \ 25 | == [b'i-2e', b'i42e', b'6:answer', b'0:'] 26 | True 27 | >>> encode([b'a', 42, [13, 14]]) == b'l1:ai42eli13ei14eee' 28 | True 29 | >>> encode({b'bar': b'spam', b'foo': 42, b'mess': [1, b'c']}) \ 30 | == b'd3:bar4:spam3:fooi42e4:messli1e1:cee' 31 | True 32 | """ 33 | 34 | if isinstance(obj, int): 35 | return b"i" + str(obj).encode() + b"e" 36 | elif isinstance(obj, bytes): 37 | return str(len(obj)).encode() + b":" + obj 38 | elif isinstance(obj, str): 39 | return encode(obj.encode("ascii")) 40 | elif isinstance(obj, list): 41 | return b"l" + b"".join(map(encode, obj)) + b"e" 42 | elif isinstance(obj, dict): 43 | if all(isinstance(i, bytes) for i in obj.keys()): 44 | items = list(obj.items()) 45 | items.sort() 46 | return b"d" + b"".join(map(encode, it.chain(*items))) + b"e" 47 | else: 48 | raise ValueError("dict keys should be bytes") 49 | raise ValueError("Allowed types: int, bytes, list, dict; not %s", type(obj)) 50 | 51 | def decode(s): 52 | """ 53 | Decodes given bencoded bytes object. 54 | 55 | >>> decode(b'i-42e') 56 | -42 57 | >>> decode(b'4:utku') == b'utku' 58 | True 59 | >>> decode(b'li1eli2eli3eeee') 60 | [1, [2, [3]]] 61 | >>> decode(b'd3:bar4:spam3:fooi42ee') == {b'bar': b'spam', b'foo': 42} 62 | True 63 | """ 64 | def decode_first(s): 65 | if s.startswith(b"i"): 66 | match = re.match(b"i(-?\\d+)e", s) 67 | return int(match.group(1)), s[match.span()[1]:] 68 | elif s.startswith(b"l") or s.startswith(b"d"): 69 | l = [] 70 | rest = s[1:] 71 | while not rest.startswith(b"e"): 72 | elem, rest = decode_first(rest) 73 | l.append(elem) 74 | rest = rest[1:] 75 | if s.startswith(b"l"): 76 | return l, rest 77 | else: 78 | return {i: j for i, j in zip(l[::2], l[1::2])}, rest 79 | elif any(s.startswith(i.encode()) for i in string.digits): 80 | m = re.match(b"(\\d+):", s) 81 | length = int(m.group(1)) 82 | rest_i = m.span()[1] 83 | start = rest_i 84 | end = rest_i + length 85 | return s[start:end], s[end:] 86 | else: 87 | raise ValueError("Malformed input.") 88 | 89 | if isinstance(s, str): 90 | s = s.encode("ascii") 91 | 92 | ret, rest = decode_first(s) 93 | if rest: 94 | raise ValueError("Malformed input.") 95 | return ret 96 | 97 | if __name__ == "__main__": 98 | import doctest 99 | doctest.testmod() 100 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. --------------------------------------------------------------------------------