Author: poeml Date: Sat May 29 14:03:18 2010 New Revision: 8067 URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain?rev=8067&view=rev Log: tools/bdecode.py added: Parse a Torrent file (or other BEncoded input), and pretty-print it. Can read from standard input: curl -s <url> | bdecode.py Useful to work on the Torrent generator in mod_mirrorbrain. Added: trunk/tools/bdecode.py (with props) Modified: trunk/tools/README Modified: trunk/tools/README URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/tools/README?rev=8067&r1=8066&r2=8067&view=diff ============================================================================== --- trunk/tools/README (original) +++ trunk/tools/README Sat May 29 14:03:18 2010 _at_@ -49,3 +49,7 @@ Tool to assess the size of rsync modules on a remote server, or a subdirectory + bdecode.py + Parse a Torrent file (or other BEncoded input), and pretty-print it. + Can read from standard input: curl -s <url> | bdecode.py + Useful to work on the Torrent generator in mod_mirrorbrain. Added: trunk/tools/bdecode.py URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/tools/bdecode.py?rev=8067&view=auto ============================================================================== --- trunk/tools/bdecode.py (added) +++ trunk/tools/bdecode.py Sat May 29 14:03:18 2010 _at_@ -1,0 +1,49 @@ +#!/usr/bin/python + +# Bencode decoder +# +# This code is in the public domain +# http://buffis.com/2007/07/28/bittorrent-bencode-decoder-in-python-using-30-lines-of-code/ + +import fileinput +from pprint import pprint + +class Decoder(object): + def __init__(self, data): self.data, self.ptr = data, 0 + def _cur(self): return self.data[self.ptr] + def _get(self, x): + self.ptr += x + return self.data[self.ptr-x:self.ptr] + def _get_int_until(self, c): + num = int(self._get(self.data.index(c, self.ptr)-self.ptr)) + self._get(1) # kill extra char + return num + def _get_str(self): return self._get(self._get_int_until(":")) + def _get_int(self): return self._get_int_until("e") + def decode(self): + i = self._get(1) + if i == "d": + r = {} + while self._cur() != "e": + key = self._get_str() + val = self.decode() + r[key] = val + self._get(1) + elif i == "l": + r = [] + while self._cur() != "e": r.append(self.decode()) + self._get(1) + elif i == "i": r = self._get_int() + elif i.isdigit(): + self._get(-1) # reeeeewind + r = self._get_str() + return r + + +lines = [] +for line in fileinput.input(): + lines.append(line) +#print repr(''.join(lines)) + +d=Decoder(''.join(lines)) +pprint(d.decode()) _______________________________________________ mirrorbrain-commits mailing list Archive: http://mirrorbrain.org/archive/mirrorbrain-commits/ Note: To remove yourself from this list, send a mail with the content unsubscribe to the address mirrorbrain-commits-request_at_mirrorbrain.orgReceived on Sat May 29 2010 - 12:03:25 GMT
This archive was generated by hypermail 2.3.0 : Mon Feb 20 2012 - 23:47:04 GMT