joalT joalT - 1 month ago 15
Python Question

Running Bram Cohen's original BitTorrent sources (Python)

I found an old source of Bram Cohen's original BitTorrent here:

http://bittorrent.cvs.sourceforge.net/viewvc/bittorrent/?hideattic=0

(says here: bittorrent source code? that it's version 3.x)

and I'm trying to run it on my Mac (10.7) and my Python version is 2.7

If you would try and download the source, you can try running the btdownloadcurses.py or the btdownloadheadless.py

I tried running:

$ ./btdownloadcurses.py --url http://sometorrenthost/somefile.torrent


Ok, I'll be more specific. This is what I did:

$ ./btdownloadcurses.py --url http://torcache.net/torrent/848A6A0EC6C85507B8370E979B133214E5B5A6D4.torrent


And this is what I got:

Traceback (most recent call last):
File "./btdownloadcurses.py", line 243, in <module>
run(mainerrlist, argv[1:])
File "./btdownloadcurses.py", line 186, in run
download(params, d.chooseFile, d.display, d.finished, d.error, mainkillflag, fieldw)
File "/Users/joal21/Desktop/BitTorrent/BitTorrent/download.py", line 120, in download
h = urlopen(config['url'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 517, in http_response
code, msg, hdrs = response.code, response.msg, response.info()
AttributeError: addinfourldecompress instance has no attribute 'msg'


When I searched for that
AttributeError
, I came to:

http://mail.python.org/pipermail/python-bugs-list/2005-May/028824.html

I think the second comment has something to do with my problem. But I don't know how else to procees from there. Am I simply just passing the wrong url? Does it have something to do with the Python version? Or of the BitTorrent source being old. Or is there something new in present .torrent files. What am I missing? Not doing?

Forgive my ignorance. I'm really at a loss here.

Answer

Bram worked against an older version of Python, one where the urllib2 code did not add .msg and .code attributes to addinfourl objects. Specifically, the Python version he developed with did not have this change applied.

The workaround is to copy those attributes yourself from the original addinfourl object in the HTTPContentEncodingHandler class found in the original zurllib.py file:

class HTTPContentEncodingHandler(HTTPHandler):
    """Inherit and add gzip/deflate/etc support to HTTP gets."""
    def http_open(self, req):
        # add the Accept-Encoding header to the request
        # support gzip encoding (identity is assumed)
        req.add_header("Accept-Encoding","gzip")
        req.add_header('User-Agent', 'BitTorrent/' + version)
        if DEBUG: 
            print "Sending:" 
            print req.headers
            print "\n"
        fp = HTTPHandler.http_open(self,req)
        headers = fp.headers
        if DEBUG: 
             pprint.pprint(headers.dict)
        url = fp.url
        resp = addinfourldecompress(fp, headers, url)
        resp.code = fp.code
        resp.msg = fp.msg
        return resp