def getMetainfo(src, openoptions='rb', style="file"): if src is None: return None metainfo = None try: metainfo_file = None # We're getting a url if style == "rawdata": return bdecode(src) elif style == "url": metainfo_file = urlopen(src) # We're getting a file that exists elif os.access(src, os.R_OK): metainfo_file = open(src, openoptions) if metainfo_file is not None: metainfo = bdecode(metainfo_file.read()) metainfo_file.close() except: print_exc() if metainfo_file is not None: try: metainfo_file.close() except: pass metainfo = None return metainfo
def _test_tlookup(self, httpseedlist): t = TorrentDef() fn = os.path.join(os.getcwd(), "file.wmv") t.add_content(fn) t.set_tracker(self.session.get_internal_tracker_url()) t.set_urllist(httpseedlist) t.finalize() wantdata = bencode(t.get_metainfo()) self.session.add_to_internal_tracker(t) #time.sleep(30) (scheme, netloc, path, pars, query, fragment) = urlparse.urlparse(self.session.get_internal_tracker_url()) urlprefix = scheme + '://' + netloc + '/tlookup?' for httpseed in httpseedlist: quoted = urllib.quote(httpseed) url = urlprefix + quoted #url = "http://www.cs.vu.nl/~arno/index.html" print >> sys.stderr, "test: Asking tracker for", url # F*ing BitTornado/Python crap: using normal urlopen here results in # an infinitely hanging read (even if read(1024)) conn = urlopen(url) gotdata = conn.read() print >> sys.stderr, "test: Tracker sent", len(gotdata) conn.close() self.assertEquals(wantdata, gotdata)
def getMetainfo(src, openoptions = 'rb', style = "file"): if src is None: return None metainfo = None try: metainfo_file = None # We're getting a url if style == "rawdata": return bdecode(src) elif style == "url": metainfo_file = urlopen(src) # We're getting a file that exists elif os.access(src, os.R_OK): metainfo_file = open(src, openoptions) if metainfo_file is not None: metainfo = bdecode(metainfo_file.read()) metainfo_file.close() except: if metainfo_file is not None: try: metainfo_file.close() except: pass metainfo = None return metainfo
def _test_tlookup(self,httpseedlist): t = TorrentDef() fn = os.path.join(os.getcwd(),"file.wmv") t.add_content(fn) t.set_tracker(self.session.get_internal_tracker_url()) t.set_urllist(httpseedlist) t.finalize() wantdata = bencode(t.get_metainfo()) self.session.add_to_internal_tracker(t) #time.sleep(30) (scheme, netloc, path, pars, query, fragment) = urlparse.urlparse(self.session.get_internal_tracker_url()) urlprefix = scheme+'://'+netloc+'/tlookup?' for httpseed in httpseedlist: quoted = urllib.quote(httpseed) url = urlprefix+quoted #url = "http://www.cs.vu.nl/~arno/index.html" print >>sys.stderr,"test: Asking tracker for",url # F*ing BitTornado/Python crap: using normal urlopen here results in # an infinitely hanging read (even if read(1024)) conn = urlopen(url) gotdata = conn.read() print >>sys.stderr,"test: Tracker sent",len(gotdata) conn.close() self.assertEquals(wantdata,gotdata)
def check_http_presence(self, hexinfohash, present): print >> sys.stderr, "test: infohash is", hexinfohash url = 'http://127.0.0.1:' + str(self.session.get_listen_port()) + '/' print >> sys.stderr, "test: tracker lives at", url f = urlopen(url) data = f.read() f.close() # WARNING: this test depends on the output of the tracker. If that # is changed, also change this. print >> sys.stderr, "test: tracker returned:", data if present: self.assert_(data.find(hexinfohash) != -1) else: self.assert_(data.find(hexinfohash) == -1)
def check_http_presence(self,hexinfohash,present): print >> sys.stderr,"test: infohash is",hexinfohash url = 'http://127.0.0.1:'+str(self.session.get_listen_port())+'/' print >> sys.stderr,"test: tracker lives at",url f = urlopen(url) data = f.read() f.close() # WARNING: this test depends on the output of the tracker. If that # is changed, also change this. print >> sys.stderr,"test: tracker returned:",data if present: self.assert_(data.find(hexinfohash) != -1) else: self.assert_(data.find(hexinfohash) == -1)
def _rerequest_single(self, t, s, l, callback): try: closer = [None] def timedout(self = self, l = l, closer = closer): if self.lock.trip(l): self.errorcodes['troublecode'] = 'Problem connecting to tracker - timeout exceeded' self.lock.unwait(l) try: closer[0]() except: pass self.externalsched(timedout, self.timeout) err = None try: if DEBUG: print >>sys.stderr,"Rerequest tracker:" print >>sys.stderr,merge_announce(t, s) h = urlopen(merge_announce(t, s), silent = True) closer[0] = h.close data = h.read() except (IOError, error), e: err = 'Problem connecting to tracker - ' + str(e) if DEBUG: print_exc() except: err = 'Problem connecting to tracker' if DEBUG: print_exc() #if DEBUG: # print >>sys.stderr,"rerequest: Got data",data try: h.close() except: pass if err: if self.lock.trip(l): self.errorcodes['troublecode'] = err self.lock.unwait(l) return if not data: if self.lock.trip(l): self.errorcodes['troublecode'] = 'no data from tracker' self.lock.unwait(l) return try: r = bdecode(data, sloppy=1) if DEBUG: print >>sys.stderr,"Rerequester: Tracker returns:", r check_peers(r) #print >>sys.stderr,"Rerequester: Tracker returns, post check done" except ValueError, e: if DEBUG: print_exc() if self.lock.trip(l): self.errorcodes['bad_data'] = 'bad data from tracker - ' + str(e) self.lock.unwait(l) return
def _rerequest_single(self, t, s, l, callback): try: closer = [None] def timedout(self=self, l=l, closer=closer): if self.lock.trip(l): self.errorcodes[ 'troublecode'] = 'Problem connecting to tracker - timeout exceeded' self.lock.unwait(l) try: closer[0]() except: pass self.externalsched(timedout, self.timeout) err = None try: if DEBUG: print >> sys.stderr, "Rerequest tracker:" print t + s h = urlopen(t + s) closer[0] = h.close data = h.read() except (IOError, error), e: err = 'Problem connecting to tracker - ' + str(e) if DEBUG: print_exc() except: err = 'Problem connecting to tracker' if DEBUG: print_exc() #if DEBUG: # print >>sys.stderr,"rerequest: Got data",data try: h.close() except: pass if err: if self.lock.trip(l): self.errorcodes['troublecode'] = err self.lock.unwait(l) return if not data: if self.lock.trip(l): self.errorcodes['troublecode'] = 'no data from tracker' self.lock.unwait(l) return try: r = bdecode(data, sloppy=1) if DEBUG: print >> sys.stderr, "Rerequester: Tracker returns:", r check_peers(r) except ValueError, e: if DEBUG: print_exc() if self.lock.trip(l): self.errorcodes[ 'bad_data'] = 'bad data from tracker - ' + str(e) self.lock.unwait(l) return