Beispiel #1
0
def file_recv(fileport, filepath):
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	sock.bind((ALL_HOST, int(fileport)))
	sock.listen(1)

	filesock, fileaddr = sock.accept()
	path = filepath.split("/")
	path = path[-1]

	try:
		os.mkdir("./download/"+Username)
	except:
		i=0
		print "[80] mkdir not ok"

	f = open("./download/"+Username+"/"+path, "wb")
	while 1:
		sheet_str = filesock.recv(1024)
		if not sheet_str: break
		
		print "[87] file recv", sheet_str 
		sheet = sheet_toForm(sheet_str)
		print "[89] file_recv_sheet", sheet.toStr()

		if sheet.Cmd=="P2PFILESEND":
			print "[100-1]"
			if sheet.Headline["CheckCode"]==ReplyCode[filepath]:
				print "[100-2]"
				msg = sheet.Body
				f.write(msg)

	f.close()
	sock.close()
Beispiel #2
0
	def p2p_recv(self, clientsock, clientaddr):

		# handshake ----------------------------------------------------
		handshake_str = clientsock.recv(1024)
		handshake = handshake_toForm(handshake_str)


		if handshake.Type != "MINET" :
			clientsock.close()
			#print "@@@ connection NOT come from Minet!"
		else:

			handshake = HANDSHAKE()
			handshake.Type = "MINET"
			handshake.Hostname = self.MY_HOST
			clientsock.send(handshake.toStr())
		# --------------------------------------------------------------
			
			while 1:
				sheet_str = clientsock.recv(1024)
				if not sheet_str: break

				print "[320]"
				print sheet_str
				print "[321]"
				sheet = sheet_toForm(sheet_str)
				msg = sheet.Body

				if sheet.Cmd=="P2PMESSAGE":
					self.fileLock.acquire()	
					input = open("./buffer/"+self.Username+"/"+sheet.Arg, "a")
					print "["+sheet.Arg+" ~> msg]" + msg
					input.write("["+sheet.Arg+"] " + msg + "\n")
					input.close()
					self.fileLock.release()
				elif sheet.Cmd=="P2PFILE":
					if sheet.Headline.has_key("Query") and sheet.Headline["Query"]=="1":
						self.RecvCode[sheet.Arg2] = sheet.Headline["SendCode"]
						print "[322] Recv file", sheet.toStr()
						input = open("./system/"+self.Username+"/"+"P2PFILE", "a")
						print "["+sheet.Arg+" ~> file]" + "<"+ sheet.Arg2 +">"
						input.write(sheet.Arg + " " + sheet.Arg2 + "\n")
						input.close()


						self.fileLock.acquire()	
						input = open("./buffer/"+self.Username+"/"+sheet.Arg, "a")
						input.write("["+sheet.Arg+"] " + " <try to send file: " + sheet.Arg2 + ">\n")
						input.close()
						self.fileLock.release()
				elif sheet.Cmd=="P2PFILEACCEPT":
					
					if self.SendCode.has_key(sheet.Arg2) and sheet.Headline["RecvCode"] == self.SendCode[sheet.Arg2]:
						file_send_thread = thread.start_new_thread(self.file_send, (self.NameList[sheet.Arg][1], sheet.Headline["FILEPORT"], sheet.Headline["ReplyCode"], sheet.Arg, sheet.Arg2, ))


				if self.NameList[sheet.Arg][0] == False:
					break

			clientsock.close()
Beispiel #3
0
def login_handle(sheet_str, clientsock, clientaddr):
	global AllSocket
	sheet = sheet_toForm(sheet_str)
	# LOGIN ----------------------------------------
	if sheet.Cmd=="LOGIN":
		print "# recv LOGIN request."
		if sheet.Arg!="" and is_off(sheet.Arg):
			NameList[sheet.Arg] = [True, clientaddr[0], sheet.Headline["Port"], time.time()]
			print "# ONLINE. [" + sheet.Arg + "]"
			status = SHEET()
			status.fill("STATUS", "1")

		else:
			status = SHEET()
			status.fill("STATUS", "0")
			if not is_off(sheet.Arg):
				status.fill_body("@@@ username is online!\n")
			
		status.headAdd("Content-Length", str(len(status.Body)))
		if status.Arg == "1":
			clientsock.send(status.toStr())							#send status
			print "# send STATUS reply. [status=" + status.Arg + "]"

			time.sleep(0.01)
			getlist_handle(clientsock)
			update_handle(sheet.Arg, "1")
			AllSocket[sheet.Arg] = clientsock
			return True, sheet.Arg
		else:
			clientsock.send(status.toStr())							
			print "# send STATUS reply. [status=" + status.Arg + "]"
	
	print "# someone LOGIN FAIL(1)."
	return False, ""
Beispiel #4
0
	def login_handle(self, clientsock):
		sheet_str = clientsock.recv(1024)
		allsheet = sheet_str.split("CS1.0")

		for sheet_str in allsheet[1:]:
			sheet = sheet_toForm(sheet_str)
		
		if sheet.Cmd=="STATUS":
			if sheet.Arg=="1":
				self.Status["login"] = True
			else:
				self.Status["login"] = False
Beispiel #5
0
	def handle(self, clientsock):

		while 1:
			sheet_str = clientsock.recv(1024)
			allsheet = sheet_str.split("CS1.0")

			for sheet_str in allsheet[1:]:
				sheet = sheet_toForm(sheet_str)
				
				# STATUS ------------------------------------------------
				if sheet.Cmd=="STATUS":
					if sheet.Arg=="1":
						print "# LOGIN SUCCEED!"
						self.Status["login"] = True
					else:
						print sheet.Body
						self.Status["login"] = False

				# LIST --------------------------------------------------
				if sheet.Cmd=="LIST":
					self.update_NameList(sheet.Body)
					print "# current LIST:"
					self.print_NameList()

				# LEAVE -------------------------------------------------
				if sheet.Cmd=="LEAVE":
					if self.Username==sheet.Arg:
						self.Status["login"] = False

				# UPDATE ------------------------------------------------
				if sheet.Cmd=="UPDATE":
					if sheet.Arg2 == "1":
						sheet = self.make_sheet(["GETLIST"])
						clientsock.send(sheet.toStr())
					else:
						self.NameList[sheet.Arg][0] = False
						self.NameList[sheet.Arg][3] = None
						self.NameList[sheet.Arg][4] = False

					self.Status["update"]+=1
					self.Status["update"]%=10000


				# CSMESSAGE ----------------------------------------------
				if sheet.Cmd=="CSMESSAGE":
					if sheet.Arg!=self.Username:
						self.print_csmessage(sheet.Body, sheet.Arg)	

			self.PROCESSING = False
Beispiel #6
0
def login_handle(clientsock):
	global Status
	sheet_str = clientsock.recv(1024)
	allsheet = sheet_str.split("CS1.0")

	for sheet_str in allsheet[1:]:
		sheet = sheet_toForm(sheet_str)
		
		if sheet.Cmd=="STATUS":
			if sheet.Arg=="1":
				print "# LOGIN SUCCEED!"
				Status["login"] = True
			else:
				print sheet.Body
				Status["login"] = False
Beispiel #7
0
def handle(clientsock):
	global Status, Username, PROCESSING

	while 1:
		sheet_str = clientsock.recv(1024)
		allsheet = sheet_str.split("CS1.0")

		for sheet_str in allsheet[1:]:
			sheet = sheet_toForm(sheet_str)
			
			# STATUS ------------------------------------------------
			if sheet.Cmd=="STATUS":
				if sheet.Arg=="1":
					print "# LOGIN SUCCEED!"
					Status["login"] = True
				else:
					print sheet.Body
					Status["login"] = False

			# LIST --------------------------------------------------
			if sheet.Cmd=="LIST":
				update_NameList(sheet.Body)
				print "# current LIST:"
				print_NameList()

			# LEAVE -------------------------------------------------
			if sheet.Cmd=="LEAVE":
				if Username==sheet.Arg:
					Status["login"] = False

			# UPDATE ------------------------------------------------
			if sheet.Cmd=="UPDATE":
				if sheet.Arg2 == "1":
					sheet = make_sheet(["GETLIST"])
					clientsock.send(sheet.toStr())
				else:
					NameList[sheet.Arg][0] = False
					NameList[sheet.Arg][3] = None
					NameList[sheet.Arg][4] = False

			# CSMESSAGE ----------------------------------------------
			if sheet.Cmd=="CSMESSAGE":
				if sheet.Arg!=Username:
					print_csmessage(sheet.Body, sheet.Arg)	


		PROCESSING = False
Beispiel #8
0
def p2p_recv(clientsock, clientaddr):

	# handshake ----------------------------------------------------
	handshake_str = clientsock.recv(1024)
	handshake = handshake_toForm(handshake_str)


	if handshake.Type != "MINET" :
		clientsock.close()
		#print "@@@ connection NOT come from Minet!"
	else:

		handshake = HANDSHAKE()
		handshake.Type = "MINET"
		handshake.Hostname = MY_HOST
		clientsock.send(handshake.toStr())
	# --------------------------------------------------------------
		
		while 1:
			sheet_str = clientsock.recv(1024)
			if not sheet_str: break

			sheet = sheet_toForm(sheet_str)

			if sheet.Cmd=="P2PMESSAGE":
				msg = sheet.Body
				print "["+sheet.Arg+" ~> msg]" + msg
			elif sheet.Cmd=="P2PFILE":
				if sheet.Headline.has_key("Query") and sheet.Headline["Query"]=="1":
					RecvCode[sheet.Arg2] = sheet.Headline["SendCode"]
					print "["+sheet.Arg+" ~> file]" + "<"+sheet.Arg2+">"
					print "# Accept reply: 'P2PFILEACCEPT " + sheet.Arg + " " + sheet.Arg2 + "' #"
					print "# non-reply indicate REJECT #"
			elif sheet.Cmd=="P2PFILEACCEPT":
			
				print "[380]",sheet.toStr()
				if SendCode.has_key(sheet.Arg2) and sheet.Headline["RecvCode"] == SendCode[sheet.Arg2]:
					# new a thread to handle file transfer
					
					file_send_thread = thread.start_new_thread(file_send, (NameList[sheet.Arg][1], sheet.Headline["FILEPORT"], sheet.Headline["ReplyCode"], sheet.Arg2,))
			
			if NameList[sheet.Arg][0] == False:
				break

		clientsock.close()
Beispiel #9
0
	def file_recv(self, fileport, filepath):

		print "[136] file recv~~~"
		sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		sock.bind((self.ALL_HOST, int(fileport)))
		sock.listen(1)

		filesock, fileaddr = sock.accept()
		path = filepath.split("/")
		path = path[-1]

		try:
			os.mkdir("./download/"+self.Username)
		except:
			i=0

		f = open("./download/"+self.Username+"/"+path, "wb")
		while 1:
			sheet_str = filesock.recv(1024)
			if not sheet_str: break

			print "[156]", sheet_str
			sheet = sheet_toForm(sheet_str)

			if sheet.Cmd=="P2PFILESEND":
				print "[100-1]"
				if sheet.Headline["CheckCode"]==self.ReplyCode[filepath]:
					print "[100-2]"
					msg = sheet.Body
					f.write(msg)

		f.close()
		sock.close()

		
		self.fileLock.acquire()	
		input = open("./buffer/"+self.Username+"/"+sheet.Arg, "a")
		input.write("["+sheet.Arg+"] " + " <finish receiving file: " + sheet.Arg2 + ">\n")
		input.close()
		self.fileLock.release()
Beispiel #10
0
def handle(sheet_str, username, clientsock):
	global NameList

	allsheet = sheet_str.split("CS1.0")
	sheet_count = len(allsheet)-1

	for sheet_str in allsheet[1:]:
		sheet = sheet_toForm(sheet_str)

		# GETLIST --------------------------------------------------------
		if sheet.Cmd=="GETLIST":
			getlist_handle(clientsock)

		# LEAVE ----------------------------------------------------------
		if sheet.Cmd=="LEAVE":
			print "# recv LEAVE request."

			NameList[username][0] = False
			print "# OFFLINE. [" + sheet.Arg + "]"
			clientsock.send(sheet.toStr())
			print "# send LEAVE reply."

			update_handle(username, "0")
			del AllSocket[username]

		# MESSAGE --------------------------------------------------------
		if sheet.Cmd=="MESSAGE":
			print "# recv MESSAGE request."
			print "# MESSAGE: '" + sheet.Body + "'"
			message_handle(clientsock, sheet.Arg, sheet.Body)

		# BEAT -----------------------------------------------------------
		if sheet.Cmd=="BEAT":
			print "# recv BEAT request. [" + username + "]"
			beat_handle(username)
			print "[233]", NameList