Пример #1
0
class Tracker():
	"""Tracker for torrents"""
	def __init__(self, ip, port=None, dbName=None):
		self.ip = ip
		self.port = port
		if dbName:
			self.db = DB(dbName).db
		else:
			self.db = None

		self.socket = None
		self.groups = {}

	# start the tracker server	
	def Start(self):
		# create a tcp thread
		self.socket = Socket(self.ip, self.port).TCP()
		self.socket.bind((self.ip, self.port))

		# now listen for any incomming connection
		print "Tracker listening at  "+ self.ip + ":"+str(self.port)
		self.socket.listen(5)

		while True:
			clientSocket, (ip, port) = self.socket.accept()
			print "Responding to ", ip, port
			clientThread = ClientThread(self, clientSocket, ip, port)
			clientThread.start()
Пример #2
0
	def __init__(self, ip=None, port=None, seeders=None, trackerAddr=None, ttl=1):
		if not (ip or port or seeders or trackerAddr) :
			print "Group required IP and port and seeders list"
			exit()

		self.trackerAddr = trackerAddr
		self.seeders = seeders
		# create a UDP socket
		print "Creating UDP group socket..."
		s1 = Socket(ip,port).UDP()
		
		# Set Time-to-live (optional)
		ttl_str = struct.pack('@i', ttl)

		# include ttl into IP header
		s1.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_str)
		
		# for udp
		# assign variable to group self
		self.gSocket = s1
		self.ip = ip
		self.port= port
		self.ttl = ttl
		self.gSocket.bind((ip, port))

		## for tcp
		port2 = 8982
		print "Creating TCP socket for seeder connection..."
		s2 = Socket(ip,port2).TCP()
		self.sSocket = s2
		self.port2 = port2
		self.sSocket.bind((ip, port2))
		print "Connecting to seeder ", seeders
		self.sSocket.connect(seeders)
		self.receivefromseeder()
Пример #3
0
class Server():
	"""Server for torrent files"""
	def __init__(self, ip, port=None, dbName=None):
		self.socket = None
		self.ip = ip
		self.port = port
		self.db = DB(dbName).db

	"""
	Start the server
	"""
	def Start(self):
		# create a tcp socket	
		self.socket = Socket(self.ip, self.port).TCP()
		self.socket.bind((self.ip, self.port))

		# listen for any incomming connections
		print "Listening at: ", (self.ip, self.port)
		self.socket.listen(5)

		while True:
			clientSocket, (ip, port) = self.socket.accept()
			print "Responding to : ", ip, port
			clientThread = ClientThread(self, clientSocket, ip, port)
			clientThread.start()
Пример #4
0
	def Die(self):
		s = Socket(self.ip, self.port2).TCP()
		s.connect(self.trackerAddr)
		resp = s.recv(256)
		if resp == "Ok":
			# send seederAddr and groupAddr to tracker.
			seederAddr = self.seeders[0] + ":" + str(self.seeders[1])
			groupAddr = self.ip + ":" + str(self.port)
			s.send(seederAddr + "||" + groupAddr)
			s.close()
			print "Group died successfully."
		else:
			print "Unable to remove group trackers database. Trying again"
			s.close()
		return 
Пример #5
0
	def GetTorrent(self, serverAddress, fileName):
		'''
		Create a TCP socket
		Connect the server
		Get the torrent file from server
		'''
		print "Downloading "+ fileName
		# Create a TCP socket
		s = Socket(self.ip, self.port).TCP()

		# connect to server
		print "Connecting to ", serverAddress
		# s.settimeout(5)   # 5 seconds
		try:
		    s.connect(serverAddress)         # "random" IP address and port
		except socket.error, exc:
		    print "Caught exception socket.error : %s" % exc
Пример #6
0
	def Start(self):
		# create a tcp thread
		self.socket = Socket(self.ip, self.port).TCP()
		self.socket.bind((self.ip, self.port))

		# now listen for any incomming connection
		print "Tracker listening at  "+ self.ip + ":"+str(self.port)
		self.socket.listen(5)

		while True:
			clientSocket, (ip, port) = self.socket.accept()
			print "Responding to ", ip, port
			clientThread = ClientThread(self, clientSocket, ip, port)
			clientThread.start()
Пример #7
0
	def Start(self):
		# create a tcp socket	
		self.socket = Socket(self.ip, self.port).TCP()
		self.socket.bind((self.ip, self.port))

		# listen for any incomming connections
		print "Listening at: ", (self.ip, self.port)
		self.socket.listen(5)

		while True:
			clientSocket, (ip, port) = self.socket.accept()
			print "Responding to : ", ip, port
			clientThread = ClientThread(self, clientSocket, ip, port)
			clientThread.start()
Пример #8
0
	def SendFile(self, file_name):

		print "Waiting for any group connection at ", self.ip, self.port, "..."
		# keep connection open, listen
		sock = Socket(self.ip, self.port).TCP()
		sock.bind((self.ip, self.port))
		sock.listen(5)

		# accept connection from tracker
		while True:
			groupSocket, (ip, port) = sock.accept()
			print "Seeding to group ", ip, port
			groupThread = ClientThread(groupSocket, ip, port, file_name)
			groupThread.start()
Пример #9
0
	def JoinGroup(self, groupIP, groupPort):
		'''
		Create a UDP socket
		Join the group
		'''

		s = Socket(self.ip, self.port).UDP()

		# Allow multiple copies of this program on one machine
		s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)


		# Bind it to the port
		s.bind((groupIP, groupPort))

		# get the group to join
		group_bin = socket.inet_pton(socket.AF_INET, groupIP)

		# Join group
		mreq = group_bin + struct.pack('=I', socket.INADDR_ANY)
		s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
	
		return s
Пример #10
0
	def UploadTorrent(self, file_name, serverAddr):
		# create a tcp 
		s = Socket(self.ip, self.port).TCP()
		
		s.bind((self.ip, self.port))
		# connect to severAddr
		print "Connecting to :", serverAddr
		s.connect(serverAddr)
		
		# upload file to server
		resp = s.recv(256)
		if resp == "Ok":
			s.send("Upload")
			resp = s.recv(256)
			if resp == "Ok":
				# convert file to .torrent file
				print "Creating torrent file."
				fp = open(file_name, 'rb')
				fileContent = fp.read(1024)
				enCodedString = Torrent().encode(fileName=file_name)
				fp.close()
				
				fp = open(file_name+".torrent", "wb")
				fp.write(enCodedString)
				fp.close()

				# send torrent file to server
				# open the file
				# send recursively
				print "Sending file to server.."
				f = open(file_name+'.torrent', 'rb')
				l = f.read(1024)
				while(l):
					s.send(l)
					l = f.read(1024)
				f.close()

				print "Waiting for resonse from server"
				resp = s.recv(1024)
				if resp == "Failed":
					print "File upload failed."
					s.close()
					# self.UploadTorrent(file_name, serverAddr)
				elif resp == "Ok":
					# file successfully uploaded. Now open socket to let users download this file
					print "File successfully uploaded."
					s.close()
					self.SendFile( file_name)
			else:
				print "Server won't let you upload"
		else:
			print "Unable to talk to server. Please try after sometime."
			s.close()
Пример #11
0
	def GetGroup(self, trackerAddr, fileName):
		'''
		Get tracker's ip and port
		Create a TCP socket and get group after doing some message passing
		'''					
		# create a tcp socket
		s = Socket(self.ip, self.port).TCP()

		s.bind((self.ip, self.port))
		
		# connect socket to tracker
		s.connect(trackerAddr)

		# get group from tracker after doing some message passing
		resp = s.recv(256)
		if resp == "Ok":
			s.send("Leecher")
			resp = s.recv(256)
			if resp == "Ok":
				s.send(fileName)
				resp = s.recv(256)
				if resp == "Ok":
					resp = s.recv(1024)
					if resp == "Failed":
						print "Unable to assign a group.."
						groupAddr =  None
					# we got the group addr	
					groupAddr = resp
				else:
					print "File not exists at server"
					groupAddr = None
		else:
			print "Tracker is not respoinding..."
			groupAddr =  None		
		
		s.close()

		return groupAddr
Пример #12
0
	def Upload(self, torrentFile):
		# decode the torrent file
		decod = Torrent().decode(torrentFile)

		# get the file name
		fileName = decod["info"]["name"]

		# get trackers 
		trackers = decod["announce"]

		# update database of itself
		print "Updating database of torrent files"
		self.server.db.torrents.insert({"fileName": fileName})

		# inform trackers to update there database
		trackerIP, trackerPort = trackers.split(":")
		trackerPort = int(trackerPort)

		print "Connecting to tracker at "+trackers
		serSoc = Socket('172.34.12.12', 6767).TCP()
		serSoc.connect((trackerIP, trackerPort))

		print "Waiting for response from racker"
		resp = serSoc.recv(256)
		if resp == "Ok":
			serSoc.send("Server")
			resp = serSoc.recv(256)
			if resp == "Ok":
				data = self.ip + ":" + str(self.port) + "||" + fileName
				print "Sending data to tracker..."
				serSoc.send(data)
				resp = serSoc.recv(256)
				if resp == "Failed":
					self.socket.send("Failed")
					return 
		else:
			self.socket.send("Failed")
			return

		# put file into the server 
		print "Putting content into a torrent file..."
		fp = open(fileName+".torrent", "wb")
		fp.write(torrentFile)
		fp.close()

		# send confirmation to client thread
		print "Sending confirmation to seeder..."
		serSoc.close()
		self.socket.send("Ok")
		return