Created
October 9, 2012 16:36
-
-
Save thomasballinger/3859927 to your computer and use it in GitHub Desktop.
my annouce function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def announce(self, torrent): | |
"""Returns data from Tracker specified in torrent""" | |
announce_query_params = { | |
'info_hash' : torrent.info_hash, | |
'peer_id' : self.client_id, | |
'port' : self.port, | |
'uploaded' : 0, | |
'downloaded' : 0, | |
'left' : torrent.length, | |
'compact' : 1, # sometimes optional | |
#'no_peer_id' : # ignored if compact enabled | |
'event' : 'started', | |
#'ip' : ip # optional | |
#'numwant' : 50 # optional | |
#'trackerid' : tracker id, if included before # optional | |
} | |
addr = torrent.announce_url | |
full_url = addr + '?' + urllib.urlencode(announce_query_params) | |
print 'opening', full_url, '...' | |
response_data = bencode.bdecode(urllib.urlopen(full_url).read()) | |
peers_data = response_data['peers'] | |
peer_addresses = [] | |
for sixbits in [peers_data[i:i+6] for i in range(0,len(peers_data),6)]: | |
peer_addresses.append(('.'.join(str(ord(ip_part)) for ip_part in sixbits[:4]), 256*ord(sixbits[4])+ord(sixbits[5]))) | |
#TODO fill out with our listening addr if we can filter it out | |
our_ip = '127.0.0.1' | |
peer_addresses = [x for x in peer_addresses if x[0] != our_ip or x[1] != self.port] | |
response_data['peers'] = peer_addresses | |
return response_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment