def processRequest(self, connectionSocket, addr): dataRec = connectionSocket.recv(1024) header, msg = protocol.decodeMsg(dataRec.decode( )) # get client's info, parse it to header and content # Main logic of the program, send different content to client according to client's requests if (header == protocol.HEAD_REQUEST): self.listFile(connectionSocket) connectionSocket.close() elif (header == protocol.HEAD_DOWNLOAD): filename = self.path + "/" + msg if os.path.isfile(filename): self.sendFile(connectionSocket, filename, addr) else: connectionSocket.send( protocol.prepareMsg(protocol.HEAD_ERROR, "File Does Not Exist")) connectionSocket.close() elif (header == protocol.HEAD_UPLOAD): uploadFilename = self.path + "/" + msg self.receiveFile(connectionSocket, uploadFilename, addr) elif (header == protocol.HEAD_SENDCHAT): self.registerRecipient( msg, addr ) #register the recipient on the server. This allows the server to retrieve that persons target IP and port later if they want to respond to a chat self.printChat(msg) connectionSocket.close() else: connectionSocket.send( protocol.prepareMsg(protocol.HEAD_ERROR, "Invalid Message")) connectionSocket.close()
def start(self): serverPort = self.port serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.bind(('', serverPort)) serverSocket.listen(20) print('The server is ready to receive') while True: connectionSocket, addr = serverSocket.accept() dataRec = connectionSocket.recv(1024) header, msg = protocol.decodeMsg(dataRec.decode()) # get client's info, parse it to header and content # Main logic of the program, send different content to client according to client's requests if (header == protocol.HEAD_REQUEST): self.listFile(connectionSocket) elif (header == protocol.HEAD_DOWNLOAD): self.sendFile(connectionSocket, self.path + "/" + msg) elif (header == protocol.HEAD_CHAT): print(msg) message = ( input("Please enter a message to send back to client")) self.chat(connectionSocket, message) elif (header == protocol.HEAD_UPLOAD): self.receiveFile(connectionSocket, self.path + "/" + msg) else: connectionSocket.send( protocol.prepareMsg(protocol.HEAD_ERROR, "Invalid Message")) connectionSocket.close()
def start(self): opt = 0 thread1 = threading.Thread( target=self.chat.chatListen ) #start the chat service on a seperate thread thread1.start() while opt != 5: opt = self.getUserSelection() if opt == 1: #list the files available on both the local disk and remote server #if(len(self.fileList)==0): #removed this line so that we could make requests against the server and get results, if the files on the server were added/removed. print('\nThese are the files available on the remote server:') self.getFileList() self.printFileList() print('\nThese are the files available on your local disk:') self.getLocalFileList() self.printFileList() elif opt == 2: self.downloadFile(self.selectDownloadFile()) elif opt == 3: self.uploadFile(self.selectUploadFile()) elif opt == 4: self.chat.sendChat() else: sock = self.connect(self.clientName, self.clientPort) #send a message to that port to wake it up, so that the while loop can terminate #this is necessary because we cannot terminate our client unless the chat thread is allowed to gracefully terminate sock.send( protocol.prepareMsg(protocol.HEAD_TERMINATECHAT, 'Exiting')) sock.close()
def uploadFile(self, fileName): #increment=0 if not os.path.isfile(self.localPath + "/" + fileName): print(fileName + " is missing from the local disk!") return mySocket = self.connect(self.serverName, self.serverPort) if mySocket is None: #If a socket could not be opened, leave gracefully return mySocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName)) time.sleep( 0.5 ) #add a slight delay here, we found in testing that if we send this request and the first file request at the same time, we can't control which arrives first, which means the server can't decide what to do with the incoming file. This helps mitigate that issue, but this is likely not an ideal solution with open(self.localPath + "/" + fileName, 'rb') as f: print(fileName + " reading initial data") l = f.read(1024) # each time we only send 1024 bytes of data while (l): mySocket.send(l) time.sleep( 0.1 ) #add an artifical delay so we can more easily watch multiple downloads/uploads from the server #print(fileName+" sending data " + str(increment)) #increment=increment+1 l = f.read(1024) print(fileName + " has been uploaded!") mySocket.close()
def chatRespond(self): while True: time.sleep( .5 ) #add a slight delay here so the messaging on the server prints out in the correct order chat = input('Say something: ') match = re.search( '^(@[^ ]+)', chat ) #match a string that starts with @, up till the first space character. This is our recipient. if not match: print( "Your chat was not formatted correctly. Start with @, followed by the user's name, a space, and the message you want to send" ) else: user = match.group() recip = recipient.recipient( user.replace('@', ''.replace(' ', '')), '', '') chat = re.sub('^(@[^ ]+)', '', chat) #strip out the recipient from the chat if recip in self.recipients: sock = self.getClientSocket( self.recipients[self.recipients.index(recip)] ) #there is likely a more efficient way to lookup a recipient object, consider refactoring sock.send( protocol.prepareMsg(protocol.HEAD_RECEIVECHAT, chat)) sock.close() print("\nMessage Sent!") else: #chat was formatted correctly, but the server could not find the recipient. print("\nI could not find a user with that name, sorry.")
def chatWithServer(self): mySocket = self.connect() sentence = input('Send message to server:') mySocket.send(protocol.prepareMsg(protocol.HEAD_CHAT, sentence)) mySocket.send(sentence.encode()) modifiedSentence = mySocket.recv(1024) print('From Server: ' + modifiedSentence.decode()) mySocket.close()
def sendChat(self): chat = input('\nWhat would you like to say? ') mySocket = self.connect(self.serverName, self.serverPort) if mySocket is None: return mySocket.send( protocol.prepareMsg(protocol.HEAD_SENDCHAT, self.constructChat(chat))) print("\nMessage Sent!")
def getFileList(self): mySocket = self.connect() mySocket.send(protocol.prepareMsg(protocol.HEAD_REQUEST, " ")) header, msg = protocol.decodeMsg(mySocket.recv(1024).decode()) mySocket.close() if (header == protocol.HEAD_LIST): files = msg.split(",") self.fileList = [] for f in files: self.fileList.append(f) else: print("Error: cannnot get file list!")
def getUploadableFileList(self): mySocket = self.connect() mySocket.send(protocol.prepareMsg(protocol.HEAD_REQUEST, " ")) header, msg = protocol.decodeMsg(mySocket.recv(1024).decode()) mySocket.close() filesInUploadList = [ f for f in os.listdir(self.uploadPath) if isfile(join(self.uploadPath, f)) ] self.uploadList = [] for f in filesInUploadList: self.uploadList.append(f)
def getFileList(self): mySocket = self.connect(self.serverName, self.serverPort) if mySocket is None: return mySocket.send(protocol.prepareMsg(protocol.HEAD_REQUEST, " ")) header, msg = protocol.decodeMsg(mySocket.recv(1024).decode()) mySocket.close() if (header == protocol.HEAD_LIST): files = msg.split(",") self.fileList = [] for f in files: self.fileList.append(f) else: print("Error: cannnot get file list, or server has no files.")
def downloadFile(self, fileName): mySocket = self.connect() mySocket.send(protocol.prepareMsg(protocol.HEAD_DOWNLOAD, fileName)) with open(self.downloadPath + "/" + fileName, 'wb') as f: print('file opened') while True: #print('receiving data...') data = mySocket.recv(1024) #print('data=%s', (data)) if not data: break # write data to a file f.write(data) print(fileName + " has been downloaded!") mySocket.close()
def sendMsg(self): mySocket = self.connect() mySocket.send(protocol.prepareMsg(protocol.HEAD_MESGREQUEST, ' ')) header, msg = protocol.decodeMsg(mySocket.recv(1024).decode()) mySocket.close() print("Client connected") while not exit(): try: msg = input() name = input() print(name + "me> " + msg) sys.stdout.flush() clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((self.serverName, self.clientPort)) clientSocket.send(msg.encode()) clientSocket.close() if msg.upper() == 'QUIT': exit() except: break
def uploadFile(self, fileName): mySocket = self.connect() mySocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName)) with open(self.uploadPath + "/" + fileName, 'wb') as f: print('file opened') while True: #print('uploading data...') bar = trange(1) data = mySocket.recv(1024) for i in bar: sleep(0.5) if not (i): tqdm.write("Uploading file %s" % fileName) #print('data=%s', (data)) if not data: break # write data to a file f.write(data) print(fileName + " has been uploaded!") mySocket.close()
def downloadFile(self, fileName): mySocket = self.connect(self.serverName, self.serverPort) if mySocket is None: #If a socket could not be opened, leave gracefully return mySocket.send(protocol.prepareMsg(protocol.HEAD_DOWNLOAD, fileName)) with open(self.localPath + "/" + fileName, 'wb') as f: print('file opened') data = mySocket.recv(1024) head, msg = protocol.decodeMsg( data.decode(errors="ignore") ) #ignore encoding errors. This stream can contain either real data, or error messages, we want to decode the bytes to find errors, and if there are none, proceed. if head == "ERR": print( fileName + " ran into a problem: " + msg ) #file download failed for some reason. Output the server error to the client. else: f.write(data) while data: #print('receiving data...') data = mySocket.recv(1024) #print('data=%s', (data)) f.write(data) # write data to a file print(fileName + " has been downloaded!") mySocket.close()
def getMessage(self, serverSocket): serverSocket.send(protocol.prepareMsg(protocol.HEAD_MSGREC))