def main(argv): if len(argv) > 2: SERVER = sys.argv[1] else: print("Benyt formatet 'python file_client.py <IP>'\n") sys.exit() sock = socket(AF_INET, SOCK_STREAM) sock.connect((SERVER, PORT)) print("Forbundet til server\n") cmd = input("Indtast filnavn. Ved afslutning skriv 'exit'\n") while cmd != "exit": Lib.writeTextTCP(cmd, sock) fileSize = Lib.getFileSizeTCP(sock) print(fileSize) if fileSize > 0: receiveFile(cmd, sock, fileSize) else: print("Fil ikke fundet\n") cmd = input("Indtast filnavn. Ved afslutning skriv 'exit'\n") Lib.writeTextTCP(cmd, sock) sock.close() sys.exit()
def requestFile(file, conn): print('Requesting file') Lib.writeTextTCP(file, conn) fileSize = Lib.getFileSizeTCP(conn) print('Size is: ', fileSize) print('Requesting,', file) Lib.writeTextTCP('Ready to receive', conn) return fileSize
def receiveFile(fileName, conn): Lib.writeTextTCP(fileName, conn) fileSize = Lib.getFileSizeTCP(conn) if fileSize != 0: fileHandle = open(fileName, "wb") toReceive = fileSize while toReceive > 0: received = conn.recv(min(toReceive, BUFSIZE)) fileHandle.write(received) toReceive -= len(received) print "Finished getting file" fileHandle.close() else: print "File '" + fileName + "' did not exist on the server"
def main(argv): clisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #checks if input is correct if len(argv) == 2: serv = argv[0] msg = argv[1] else: print("Formatet skal vaere <server IP> <sti/Filnavn>") sys.exit() #client connects to server clisock.connect((serv, PORT)) print("Connection til server etableret") #writes the filename t the server Lib.writeTextTCP(msg, clisock) #gets the filsize size = Lib.getFileSizeTCP(clisock) print("filstorelsen er: {}".format(size)) dataread = 0 #checks if the file exsists and if it does it starts receiving the file if size > 0: filename = Lib.extractFilename(msg) print("Filnavnet er: {}".format(msg)) file = open(filename, 'wb') while dataread < size: filedata = clisock.recv(BUFSIZE) file.write(filedata) dataread = dataread + len(filedata) print("filen er faerdigoverfort") else: print("File not found on server") sys.exit() clisock.close()
def receiveFile(fileName, conn): fileSize = Lib.getFileSizeTCP(conn) if fileSize == 0: #checks if file exist print("Filesize is 0. File doesn't exist") conn.close() #close connection to the server else: print 'Filesize is: ', fileSize print("Starting download") f = open("fileName", "wb") #open file or create. write binary. receivedData = conn.recv( BUFSIZE) #Reads the first 1000 bytes sent from server f.write(receivedData) #write the first chunk of the file receivedTotal = len(receivedData) #sets the total number of bytes recieved while receivedTotal < fileSize: #runs until total number of recieved bytes is larger or equal to filesize receivedData = conn.recv( BUFSIZE) #reads chunks of 1000 bytes sent from server f.write(receivedData) #writes the received data to file receivedTotal += len( receivedData) #keeps count on the total number og bytes received. print("Dowload completed. Client will close") conn.close() #close connection to server.