Beispiel #1
0
def tracker_request(filename, no_dns):	
	h = bencoding.url_hash(filename)
	torrent_file = open(filename, "rb")
	torrent_data = torrent_file.read()
	
	(length, metadata) = bencoding.bencode_parse(torrent_data)
	
	track = bencoding.http_trackers(metadata)
	
	if len(track) == 0:
		raise Exception("No HTTP trackers found (engine does not currently support UDP trackers)")
	
	print "Using tracker at " + track[0]

	peer_id = generate_peer_id()

	resp = generate_request(track, h, peer_id, "")

	print resp
	
	(x, r) = parse_tracker_response(resp)
	d = bencoding.bencode_dict_no_tod(r)
	
	peers = ""
	
	try:
		peers = d["peers"]
	except KeyError:
		try:
			tmp = d["peers6"]
		except KeyError:
			raise Exception("Didn't find peers")
		raise Exception("IPv6 not supported!")

	l = [None] * (len(peers) / 6)

	for i in range(0, len(peers) / 6):
		ip = str(ord(peers[i * 6 + 0])) + '.' \
		   + str(ord(peers[i * 6 + 1])) + '.' \
		   + str(ord(peers[i * 6 + 2])) + '.' \
 		   + str(ord(peers[i * 6 + 3])) 

		port = str(((ord(peers[i * 6 + 4]) << 8) + ord(peers[i * 6 + 5])))
		l[i] = (ip, port)
	
	# l.sort()	
	
	#for i in range(0, len(l)):
		#addr = l[i]
		#ip, port = addr.split(":")
		#s = addr 
		#try:
			#if not no_dns:
				#name, alias, address = socket.gethostbyaddr(ip)
				#l[i] = addr + " (" + name + ") "
		#except Exception:
			#pass
	# 	print addr
	
	return l
	def __init__(self, filename):
		self.name = filename 									# name of file
		self.f = open(filename, "rb")							# Actual underlying file object
		self.data = self.f.read()								# Contents of file
		self.info_hash = bencoding.info_hash(filename)			# Hash of the info section of the torrent file
		(self.length, d) = bencoding.bencode_parse(self.data)	# Python object version of torrent file
		self.dictionary = bencoding.bencode_dict_no_tod(d)		# Python object version of torrent file
		self.trackers = bencoding.trackers(self.dictionary) 	# List of trackers