def client_get(sock, file_name): ''' downloads file from server to client ''' # receive valid file indicator from server file_check_size = int(data_handling.receive_data(sock, HEADER_SIZE)) file_check = data_handling.receive_data(sock, file_check_size) # check if file in server_files if file_check == '/FileNotFound': print(f'{file_name} does not exist in server') data_handling.send_data(sock, '/FileNotFound') return # get file size file_size = int(data_handling.receive_data(sock, HEADER_SIZE)) print(f'Downloading {file_name} (Size: {file_size} B) from server...') # receive file data file_data = data_handling.receive_data(sock, file_size) # create file with received data in client_files directory file_path = os.path.join(CLIENT_FILES_DIR, file_name) file = open(file_path, 'w') file.write(file_data) file.close() print(f'Succesfully downloaded {file_name} from server')
def client_ls(sock): ''' lists files on server ''' files_list_size = int(data_handling.receive_data(sock, HEADER_SIZE)) files_list = data_handling.receive_data(sock, files_list_size) for file in files_list.split('/'): print(file)
def run(args): # To run: python3 serv.py <PORT> if len(args) != 2: print("Usage: python3 " + args[0] + " <PORT>") sys.exit() port = args[1] # bind socket to a port servSocket = bind(int(port)) if not servSocket: print("Failed bind to a port") sys.exit() while True: # keep listening for connections until user ends process print("Listening on port " + port) cliSocket, addr = servSocket.accept() print("Connected to: " + addr[0]) while True: query = receive_data(cliSocket, const.HEADER_SIZE) # print(query) # get <FILE NAME> # sends <FILE NAME> to client if query == const.COMMANDS[0]: giveFromServer(cliSocket) # put <FILE NAME> # downloads <FILE NAME> from client elif query == const.COMMANDS[1]: receive_file(cliSocket) # ls # lists files on the server elif query == const.COMMANDS[2]: # get file names from folder files = os.listdir(const.SERVER_FOLDER) response = "" for file in files: response += file + " " response = response[:-2] # send response responseSize = size_padding(len(response), const.HEADER_SIZE) data = responseSize + response send_data(cliSocket, data) # quit # closes the connection elif query == const.COMMANDS[3]: cliSocket.close() print("Connection closed") break else: print("Invalid command. Closing connection") cliSocket.close() break
def receive_file(sock): # create new socket for data transfer servSocket = bind() # send port number to client servPort = servSocket.getsockname()[1] send_data(sock, str(servPort)) # accept client connection print("Listening on port " + str(servPort)) dataSocket, addr = servSocket.accept() print("Connected to: " + addr[0]) # get payload sizes fileNameSize = receive_data(dataSocket, const.HEADER_SIZE) fileDataSize = receive_data(dataSocket, const.HEADER_SIZE) # error checking if fileNameSize == "": print("Failed to receive file name size") return if fileDataSize == "": print("Failed to receive file data size") return # read payload fileName = receive_data(dataSocket, int(fileNameSize)) fileData = receive_data(dataSocket, int(fileDataSize)) # write file filePath = const.SERVER_FOLDER + fileName userFile = open(filePath, "w") userFile.write(fileData) print(fileName + " received") print("File size: " + fileDataSize) # close file and connection userFile.close() dataSocket.close() print("Data transfer connection closed")
def server_put(sock, file_name): ''' uploads file to server from client ''' # receive valid file indicator from client file_check_size = int(data_handling.receive_data(sock, HEADER_SIZE)) file_check = data_handling.receive_data(sock, file_check_size) # check if file in client_files if file_check == '/FileNotFound': return 0 # receive file size from client file_size = int(data_handling.receive_data(sock, HEADER_SIZE)) # receive file from client file_data = data_handling.receive_data(sock, file_size) # create file with received data in server_files directory file_path = os.path.join(SERVER_FILES_DIR, file_name) file = open(file_path, 'w') file.write(file_data) file.close() return 1
def put_file(sock, address, fileName): # grab files only from client folder filePath = const.CLIENT_FOLDER + fileName # open file and get file size try: userFile = open(filePath, "r") fileSize = os.path.getsize(filePath) except Exception as e: print(e) # print("Failed to open file") return # get data channel port number from server dataPort = receive_data(sock, const.HEADER_SIZE) # connect to new port dataSocket = connect(address, int(dataPort)) # if connection failed, exit if not dataSocket: print("Failed to connect to server") return # make file size and file name headers fileNameSize = size_padding(len(fileName), const.HEADER_SIZE) fileDataSize = size_padding(fileSize, const.HEADER_SIZE) fileData = userFile.read() # add headers to payload data = fileNameSize + fileDataSize + fileName + fileData # send data send_data(dataSocket, data) print(fileName + " upload successful.") print("Bytes sent: " + str(len(data))) # close file and connection userFile.close() dataSocket.close() print("Data transfer connection closed")
def main(): # check correct number of arguments if len(sys.argv) < 2: print(f'USAGE: $ python {sys.argv[0]} <PORT NUMBER>') exit() # listening port server_port = sys.argv[1] # create a socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # bind socket to port server_socket.bind(('', int(server_port))) # start listening on socket server_socket.listen(1) # accept connections until interrupt (CTRL+C) while True: print('Waiting for connections...') # accept connection client_socket, client_addr = server_socket.accept() print(f'Accepted connection from client: {client_addr}') # keep connection open until client issues 'quit' command while True: # receive command header cmd_size = int( data_handling.receive_data(client_socket, HEADER_SIZE)) # receive command cmd = data_handling.receive_data(client_socket, cmd_size) # split 'cmd' so that: # cmd[0]: command # cmd[1]: <FILE NAME> if exists cmd = cmd.split() # get command if cmd[0] == cmds.CMDS[0]: print(f'Executing command: {cmd[0]} {cmd[1]}...') check = cmds.server_get(client_socket, cmd[1]) # check if download successful if check: print( f'Client successfully downloaded {cmd[1]} from server') else: print(f'{cmd[1]} does not exist') # put command elif cmd[0] == cmds.CMDS[1]: print(f'Executing command: {cmd[0]} {cmd[1]}...') check = cmds.server_put(client_socket, cmd[1]) # check if download successful if check: print(f'Client successfully uploaded {cmd[1]} to server') else: print(f'{cmd[1]} does not exist') # ls command elif cmd[0] == cmds.CMDS[2]: print(f'Executing command: {cmd[0]}...') cmds.server_ls(client_socket) print('Client successfully received list of files in server') # quit command elif cmd[0] == cmds.CMDS[3]: print(f'Executing command: {cmd[0]}...') print('Client disconnected') break # close socket client_socket.close()
def run(args): # To run: python3 cli.py <SERVER> <PORT> if len(args) != 3: print("Usage: python3 " + args[0] + " <SERVER> <PORT>") sys.exit() server = args[1] port = args[2] # connect to server cliSocket = connect(server, int(port)) if not cliSocket: print("Failed to connect to " + server) sys.exit() query = "" while True: query = (input("ftp> ")).lower().split() # print(query) # get <FILE NAME> # downloads <FILE NAME> from the server if query[0] == const.COMMANDS[0]: if len(query) != 2: print("Usage: get <FILE NAME>") else: send_data(cliSocket, query[0]) getFromServer(cliSocket, query[1]) # put <FILE NAME> # uploads <FILE NAME> to the server elif query[0] == const.COMMANDS[1]: if len(query) != 2: print("Usage: put <FILE NAME>") else: send_data(cliSocket, query[0]) put_file(cliSocket, server, query[1]) # ls # lists files on the server elif query[0] == const.COMMANDS[2]: # send query send_data(cliSocket, query[0]) # get size of response responseSize = receive_data(cliSocket, const.HEADER_SIZE) if responseSize == "": print("Failed to receive size of response") else: response = receive_data(cliSocket, int(responseSize)) print(response) # quit # disconnects from the server and exits elif query[0] == const.COMMANDS[3]: send_data(cliSocket, query[0]) cliSocket.close() print("Connection closed") break else: print("Invalid command")