fuzeman / bencode.py Goto Github PK
View Code? Open in Web Editor NEWSimple bencode parser (for Python 2, Python 3 and PyPy)
License: Other
Simple bencode parser (for Python 2, Python 3 and PyPy)
License: Other
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.
While preparing a Gentoo ebuild for this package, I encountered the following setuptools warnings:
The first three should be simple character replacements - don't know about the last one...
$ 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)
The documentation is inconsistent as to read/bread, write/bwrite, but it doesn't matter as none of them exist.
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'>
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))
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)
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
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'>
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), ...
Might it be possible to switch to the PSF license?
How can I determine the bencode module version programmatically. I don't see bencode.__version__
or bencode.get_version()
or equiv.
Is this project licensed under BitTorrent version 1.1 or version 1.0?
Line 3: Version 1.1
Line 140: The contents of this file are subject to the BitTorrent Open Source License Version 1.0
Hi,
could you please add license text along the sources in both the github repo and pypi release tarball?
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.