Giter Site home page Giter Site logo

bencode.py's Introduction

bencode.py

image

image

image

Simple bencode parser (for Python 2, Python 3 and PyPy), forked from the bencode package by Thomas Rampelberg.

Usage

Encode:

>>> import bencodepy

>>> bencodepy.encode({'title': 'Example'})
b'd5:title7:Examplee'

>>> bencodepy.encode(12)
b'i12e'

Decode:

>>> import bencodepy

>>> bencodepy.decode('d5:title7:Examplee')
{b'title': b'Example'}

>>> bencodepy.decode('i12e')
12

Decode to UTF-8:

>>> import bencodepy

>>> bc = bencodepy.Bencode(
    encoding='utf-8'
)

>>> bc.decode('d5:title7:Examplee')
{'title': 'Example'}

bencode

(legacy, backwards-compatible package)

This package will continue to be provided for backwards-compatibility, but upgrading to bencodepy is recommended for more reliable decoding results.

Under-the-hood this just provides proxies to a Bencode instance created with:

Bencode(
    encoding='utf-8',
    encoding_fallback='value',
    dict_ordered=True,
    dict_ordered_sort=True
)

Encode:

>>> import bencode

>>> bencode.encode({'title': 'Example'})
'd5:title7:Examplee'

>>> bencode.encode(12)
'i12e'

Decode:

>>> import bencode

>>> bencode.decode('d5:title7:Examplee')
OrderedDict([(u'title', u'Example')])

>>> bencode.decode('i12e')
12

API

bencodepy.Bencode(encoding=None, encoding_fallback=None, dict_ordered=False, dict_ordered_sort=False)

Create instance

  • encoding

    Encoding to decode strings with (or None for binary)

  • encoding_fallback

    Fallback to binary when decoding fails on the specified string types.

    • key - dictionary keys
    • value - values
    • all - always fallback to binary
    • None - always raise decoding errors
  • dict_ordered

    Use OrderedDict

  • dict_ordered_sort

    Ensure OrderedDict is sorted

Methods:

  • decode(value)

    Decode bencode string value.

  • encode(value)

    Encode value into a bencode string.

  • read(fd)

    Decode bencode from file or path fd.

  • write(data, fd)

    Encode data to file or path fd.

bencodepy.BencodeDecoder(encoding=None, encoding_fallback=None, dict_ordered=False, dict_ordered_sort=False)

Create decoder

  • encoding

    Encoding to decode strings with (or None for binary)

  • encoding_fallback

    Fallback to binary when decoding fails on the specified string types.

    • key - dictionary keys
    • value - values
    • all - always fallback to binary
    • None - always raise decoding errors
  • dict_ordered

    Use OrderedDict

  • dict_ordered_sort

    Ensure OrderedDict is sorted

Methods:

  • decode(value)

    Decode bencode string value.

bencodepy.BencodeEncoder()

Create encoder

Methods:

  • encode(value)

    Encode value into a bencode string.

bencodepy.bencode(value)

bencodepy.encode(value)

Encode value into a bencode string with the default encoder.

bencodepy.bdecode(value)

bencodepy.decode(value)

Decode bencode string value with the default decoder.

bencodepy.bread(fd)

Decode bencode from file or path fd with the default decoder.

bencodepy.bwrite(data, fd)

Encode data to file or path fd with the default encoder.

bencode

bencode.bencode(value)

bencode.encode(value)

Encode value into the bencode format.

bencode.bdecode(value)

bencode.decode(value)

Decode bencode formatted string value.

bencode.bread(fd)

Read bencode formatted string from file or path fd.

bencode.bwrite(data, fd)

Write data as a bencode formatted string to file or path fd.

bencode.py's People

Contributors

dvf avatar exsidius avatar fuzeman avatar lostnihilist avatar wgh- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bencode.py's Issues

License

Might it be possible to switch to the PSF license?

Unnecessary data when saving to a file

Thank you, after updating the problems with saving there, but when saving, there are some extra data at the beginning of the file

r = requests.get(torrent, headers={'Host':'tracktor.in'})
torrent = r.text
f = open(filename, 'w+')
f.write(bencode.bencode(torrent))
f.close()
print torrent

When the torrent data is output to the console, I see the beginning of the file as:

d8:announce76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announce13:announce-listll76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceel77:http://bt99.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceee7:comment14:LostFilm.TV(c)10:created by13:uTorrent/331013:creation

but after saving to a file, the file itself has some extra data

**38149:**d8:announce76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announce13:announce-listll76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceel77:http://bt99.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceee7:comment14:LostFilm.TV(c)10:created by13:uTorrent/331013:creation

I do not see in my code what this garbage could give

setuptools warnings

While preparing a Gentoo ebuild for this package, I encountered the following setuptools warnings:

  • Usage of dash-separated 'author-email' will not be supported in future versions. Please use the underscore name 'author_email' instead
  • Usage of dash-separated 'description-file' will not be supported in future versions. Please use the underscore name 'description_file' instead
  • Usage of dash-separated 'home-page' will not be supported in future versions. Please use the underscore name 'home_page' instead
  • easy_install command is deprecated. Use build and pip and other standards-based tools.

The first three should be simple character replacements - don't know about the last one...

encode_string has inconsistent return behavior

note how encode_string does return in the error code path, while the default code path extends the r argument.

def encode_string(x, r):
    try:
        s = x.encode('utf-8')
    except UnicodeDecodeError:
        return encode_bytes(x, r)

    r.extend((str(len(s)).encode('utf-8'), b':', s))

add license text

Hi,
could you please add license text along the sources in both the github repo and pypi release tarball?

Keys in bencode 3 are bytes, where in bencode 2 they were strings?

Keys in bencode 3 are bytes, wheres in bencode 2 they were strings? Is this the intended behavior?

Bencode 2:

import bencode
with open('test_data/bencode_transmission', 'rb') as f: 
  x = bencode.bread(f)

print(x)
OrderedDict([('activity-date', 1383935064), ('added-date', 1383924680), ...

Bencode 3:

import bencode
with open('test_data/bencode_transmission', 'rb') as f: 
  x = bencode.bread(f)

print(x)
OrderedDict([(b'activity-date', 1383935064), (b'added-date', 1383924680), ...

Dict with bytes as keys?

I don't get why force_decode_utf8 is set to True for key reading in decode_dict. I think it's perfectly valid to have a dict which keys are byte sequences.
In fact I've come across some files from trackers which do encode hashes as bytes to save some space and therefore fail to parse using this library.
I'd send a PR but it is pretty straightforward change

AttributeError: 'module' object has no attribute 'encode'

Hey,

I'm having some difficulty in getting bencode working:

pip install bencode.py
import bencode

bencode.encode({'title': 'Example'})

# ---------------------------------------------------------------------------
# AttributeError                            Traceback (most recent call last)
# <ipython-input-17-f26380d80be9> in <module>()
#       3 import bencode
#       4 
# ----> 5 bencode.encode({'title': 'Example'})

# AttributeError: 'module' object has no attribute 'encode'

I would really appreciate a pointer in where I'm going wrong.

Btw, I'm using Python 2.7.10.

KeyError: <type 'unicode'>

Hello. help please with saving the torrent file

r = requests.get(torrent, headers={'Host':'tracktor.in'})
bt = bencode.bdecode(r.text)
			
print bt
			
f = open(filename, 'w+')
f.write(bencode.encode(bt))

but i have error
KeyError: <type 'unicode'>
and I do not understand what exactly is wrong

{u'comment': u'LostFilm.TV(c)', u'info': {u'piece length': 524288, u'name': u'Van.Helsing.S02E08.rus.LostFilm.TV.avi', u'private': 1, u'pieces':'...', u'source': u'LostFilm.TV', u'length': 468131840, u'profiles': [{u'width': 720, u'vcodec': u'XVID', u'height': 400, u'acodec': u'mp3'}], u'file-duration': [2581], u'file-media': [0]}, u'encoding': u'UTF-8', u'creation date': 1512330386, u'announce-list': [[u'http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announce'], [u'http://bt99.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announce']], u'created by': u'uTorrent/3310', u'announce': u'http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announce'}
Van.Helsing.S02E08.avi.torrent
Traceback (most recent call last):
File "index.py", line 88, in
l.getRss()
File "index.py", line 54, in getRss
self.getSerialPage(url)
File "index.py", line 84, in getSerialPage
f.write(bencode.encode(bt))
File "/Library/Python/2.7/site-packages/bencode/init.py", line 200, in bencode
encode_func[type(value)](value, r)
File "/Library/Python/2.7/site-packages/bencode/init.py", line 156, in encode_dict
encode_func[type(v)](v, r)
KeyError: <type 'unicode'>

Decoding of example fails

$ python3
Python 3.7.7 (default, Mar 13 2020, 10:23:39) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bencode
>>> bencode.decode('d5:title7:Examplee')
Traceback (most recent call last):
  File "/home/da/.local/lib/python3.7/site-packages/bencode/__init__.py", line 180, in bdecode
    r, l = decode_func[value[0:1]](value, 0)
KeyError: 'd'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/da/.local/lib/python3.7/site-packages/bencode/__init__.py", line 182, in bdecode
    raise BencodeDecodeError("not a valid bencoded string")
bencode.exceptions.BencodeDecodeError: not a valid bencoded string

$ pip3 install --user bencode.py
Requirement already satisfied: bencode.py in ./.local/lib/python3.7/site-packages (2.1.0)

bdecode fails on python3 with bytes objects

bencoded files can, and in their most common use case do, include raw binary data. The native string type in python 2 is just binary data (str). This means loading a torrent file, and decoding it with python 2 works just fine with bencode.py (as it uses uses the native string type exclusively internally).

In python 3, the native string type is a sequence of unicode code points, not bytes, and never the twain shall they meet. So while a unicode string (python 3 str) will encode and decode just fine with bencode.py, it cannot decode or encode a bytes structures. ...And you need to be able to do that to handle torrent files (which cannot be represented in unicode - there is raw binary in the file)

How to determine the bencode module version programmatically

How can I determine the bencode module version programmatically. I don't see bencode.__version__ or bencode.get_version() or equiv.

  1. Is there a way to determine the bencode module version programmatically? (without having to rely on an external module like pkg_resources)
  2. If so how?

Encoder does not support base type - None

Version: bencode.py==2.1.0

Test:

bencode.bencode(None)

expected: b'0:'
actual:

>>> bencode.bencode(None)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "./.env/lib/python3.8/site-packages/bencode/__init__.py", line 304, in bencode
   encode_func[type(value)](value, r)
KeyError: <class 'NoneType'>

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.