def _announce(self, event=None): assert not self.dead self.current_started = bttime() self.errorfunc(logging.INFO, 'announce: ' + str(bttime() - self.current_started)) s = ('%s&uploaded=%s&downloaded=%s&left=%s' % (self.url, str(self.up()*self.config.get('lie',1) - self.previous_up), str(self.down() - self.previous_down), str(self.amount_left()))) if self.last is not None: s += '&last=' + quote(str(self.last)) if self.trackerid is not None: s += '&trackerid=' + quote(str(self.trackerid)) if self.howmany() >= self.config['max_initiate']: s += '&numwant=0' else: s += '&compact=1' if event is not None: s += '&event=' + event self._rerequest(s)
def encode_for_filesystem(path): assert isinstance(path, unicode), "Path should be unicode not %s" % type(path) bad = False encoding = get_filesystem_encoding() if encoding == None: encoded_path = path else: try: encoded_path = path.encode(encoding) except: bad = True path.replace(u"%", urllib.quote(u"%")) encoded_path = path.encode(encoding, 'urlquote') return (encoded_path, bad)
def _makeurl(self, peerid, port): return ('%s?info_hash=%s&peer_id=%s&port=%s&key=%s' % (self.baseurl, quote(self.announce_infohash), quote(peerid), str(port), b2a_hex(''.join([chr(random.randrange(256)) for i in xrange(4)]))))
def natchecklog(self, peerid, ip, port, result): print isotime(), '"!natcheck-%s:%i" %s %i 0 - -' % ( ip, quote(peerid), port, result)
def get_infopage(self): try: if not self.config['show_infopage']: return (404, 'Not Found', default_headers, alas) red = self.config['infopage_redirect'] if red != '': return (302, 'Found', {'Content-Type': 'text/html', 'Location': red}, '<A HREF="'+red+'">Click Here</A>') s = StringIO() s.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n' \ '<html><head><title>BitTorrent download info</title>\n') if self.favicon is not None: s.write('<link rel="shortcut icon" href="/favicon.ico">\n') s.write('</head>\n<body>\n' \ '<h3>BitTorrent download info</h3>\n'\ '<ul>\n' '<li><strong>tracker version:</strong> %s</li>\n' \ '<li><strong>server time:</strong> %s</li>\n' \ '</ul>\n' % (version, isotime())) if self.allowed is not None: if self.show_names: names = [ (value['name'], infohash) for infohash, value in self.allowed.iteritems()] else: names = [(None, infohash) for infohash in self.allowed] else: names = [ (None, infohash) for infohash in self.downloads] if not names: s.write('<p>not tracking any files yet...</p>\n') else: names.sort() tn = 0 tc = 0 td = 0 tt = 0 # Total transferred ts = 0 # Total size nf = 0 # Number of files displayed if self.allowed is not None and self.show_names: s.write('<table summary="files" border="1">\n' \ '<tr><th>info hash</th><th>torrent name</th><th align="right">size</th><th align="right">complete</th><th align="right">downloading</th><th align="right">downloaded</th><th align="right">transferred</th></tr>\n') else: s.write('<table summary="files">\n' \ '<tr><th>info hash</th><th align="right">complete</th><th align="right">downloading</th><th align="right">downloaded</th></tr>\n') for name, infohash in names: l = self.downloads[infohash] n = self.completed.get(infohash, 0) tn = tn + n c = self.seedcount[infohash] tc = tc + c d = len(l) - c td = td + d nf = nf + 1 if self.allowed is not None and self.show_names: if self.allowed.has_key(infohash): sz = self.allowed[infohash]['length'] # size ts = ts + sz szt = sz * n # Transferred for this torrent tt = tt + szt if self.allow_get == 1: linkname = '<a href="/file?info_hash=' + quote(infohash) + '">' + name + '</a>' else: linkname = name s.write('<tr><td><code>%s</code></td><td>%s</td><td align="right">%s</td><td align="right">%i</td><td align="right">%i</td><td align="right">%i</td><td align="right">%s</td></tr>\n' \ % (b2a_hex(infohash), linkname, size_format(sz), c, d, n, size_format(szt))) else: s.write('<tr><td><code>%s</code></td><td align="right"><code>%i</code></td><td align="right"><code>%i</code></td><td align="right"><code>%i</code></td></tr>\n' \ % (b2a_hex(infohash), c, d, n)) ttn = 0 for i in self.completed.itervalues(): ttn = ttn + i if self.allowed is not None and self.show_names: s.write('<tr><td align="right" colspan="2">%i files</td><td align="right">%s</td><td align="right">%i</td><td align="right">%i</td><td align="right">%i/%i</td><td align="right">%s</td></tr>\n' % (nf, size_format(ts), tc, td, tn, ttn, size_format(tt))) else: s.write('<tr><td align="right">%i files</td><td align="right">%i</td><td align="right">%i</td><td align="right">%i/%i</td></tr>\n' % (nf, tc, td, tn, ttn)) s.write('</table>\n' \ '<ul>\n' \ '<li><em>info hash:</em> SHA1 hash of the "info" section of the metainfo (*.torrent)</li>\n' \ '<li><em>complete:</em> number of connected clients with the complete file</li>\n' \ '<li><em>downloading:</em> number of connected clients still downloading</li>\n' \ '<li><em>downloaded:</em> reported complete downloads (total: current/all)</li>\n' \ '<li><em>transferred:</em> torrent size * total downloaded (does not include partial transfers)</li>\n' \ '</ul>\n') s.write('</body>\n' \ '</html>\n') return (200, 'OK', {'Content-Type': 'text/html; charset=iso-8859-1'}, s.getvalue()) except: print_exc() return (500, 'Internal Server Error', {'Content-Type': 'text/html; charset=iso-8859-1'}, 'Server Error')
def urlquote_error(error): s = error.object[error.start:error.end] s = s.encode('utf8') s = urllib.quote(s) s = s.decode('ascii') return (s, error.end)