def main(argv): serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((HOST, PORT)) serverSocket.listen(1) while 1: print("The server is ready to accept...") connectionSocket, addr = serverSocket.accept() print("Socket accept", addr) fileName = Lib.readTextTCP(connectionSocket) #Modtager sti og filnavn fra client #fileName2 = Lib.extractFilename(fileName) print("Message received from client:", fileName) fileExists = Lib.check_File_Exists(fileName) fileExists2 = str(fileExists) #if fileExists != 0: #returns fileSize #file = open(fileName2,'rb') #opens file without truncation #file2 = file.read(all) #fileSize = Lib.getFileSizeTCP(connectionSocket) Lib.writeTextTCP(fileExists2, connectionSocket) sendFile(fileName, fileExists, connectionSocket) #else: #Lib.writeTextTCP("File not found", connectionSocket) #Sender error message til client connectionSocket.close()
def main(argv): #creating an INET an streaming socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #bind to the HOST id and port 9000 sock.bind((HOST, PORT)) #The server can queue up to 5 connect requests sock.listen(5) print("Server socket opened") while True: #accepts connection from client clisock, addr = sock.accept() print("connection from {} achieved".format(addr)) #reads the message from the client msg = Lib.readTextTCP(clisock) print("received following message: {} ".format(msg)) #if the message is close it will close the connections and exit if msg == "close": sock.close() print("Server socket closed") clisock.close() sys.exit() #checks if the file exists and returns the size of the file, if it doesn't exist it returns 0 size = Lib.check_File_Exists(msg) if size > 0: print("The size of the file is: {} bit".format(size)) #converts the size of the file to a string and sends it to the client returnmsg = str(size) Lib.writeTextTCP(returnmsg, clisock) #opens the requested file file = open(msg, 'rb') #reads BUFSIZE bits from the file filedata = file.read(BUFSIZE) #while there is data to be read it will send the data and read new data while filedata: clisock.send(filedata) filedata = file.read(BUFSIZE) else: print("The file doesn't exist") #converts the size of the file to a string and sends it to the client returnmsg = str(size) Lib.writeTextTCP(returnmsg, clisock) clisock.close()
def main(argv): serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serverSocket.bind((HOST, PORT)) serverSocket.listen(1) print 'Server setup done' while (True): connectionSocket, addr = serverSocket.accept() fileName = Lib.readTextTCP(connectionSocket) fileSize = Lib.check_File_Exists(fileName) sendFile(fileName, fileSize, connectionSocket) connectionSocket.close()
def main(argv): serverSock = socket(AF_INET, SOCK_STREAM) #create TCP socket serverSock.bind((HOST, PORT)) #Bind socket to IP og port(address) serverSock.listen(1) #waiting for client to connect. print 'The server is ready to receive' while True: conn, addr = serverSock.accept() #completes connection with client print 'Connected to client - Port - ', PORT txtClient = Lib.readTextTCP( conn) #Reads file and directory from client fileName = Lib.extractFilename(txtClient) fileSize = str(Lib.check_File_Exists(fileName)) Lib.writeTextTCP(fileSize, conn) #send filesize to client. sendFile(fileName, fileSize, conn) #calls sendfile()
def main(argv): # TO DO Your Code serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind(('', PORT)) serverSocket.listen(1) print 'The server is ready to receive' while 1: connectionSocket, addr = serverSocket.accept() file = Lib.readTextTCP(connectionSocket) filename = Lib.extractFilename(file) size = str(Lib.check_File_Exists(filename)) sendFile(filename, size, connectionSocket) serverSocket.close()
def sendFile(fileName, clientAddr, server): try: # send size af fil msg = "File size: " + str(Lib.check_File_Exists(fileName)) server.sendto(msg.encode(), clientAddr) except: # send fejl besked server.sendto("File not found!".encode(), clientAddr) with open(fileName, "rb") as file: # læs fra filen data = file.read(HEADER) # læs første 1000 bytes print("Sending...") while data: # send mens der er data tilbage server.sendto(data, clientAddr) data = file.read(HEADER) print("File sent.")
def sendFile(fileName, conn): try: # prøv at sende størrelse af fil... msg = "File size: " + str(Lib.check_File_Exists(fileName)) conn.send(msg.encode()) except: # hvis det mislykkes. send en fejl besked conn.send("File not found!".encode()) with open(fileName, "rb") as file: # åben fil data = file.read(HEADER) # læs de første 1000 bytes print("Sending...") while data: # læs data indtil der ikke er mere conn.send(data) # send til klient data = file.read(HEADER) # læs næste 1000 bytes print("File sent.")
def main(argv): # TO DO Your Code # Create a TCP/IP socket serverSocket = socket(AF_INET, SOCK_STREAM) # Bind the socket to the port server_address = (HOST, PORT) serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) serverSocket.bind(server_address) # Listen for incoming connections serverSocket.listen(1) print("Server are ready to recieve") # loop waiting for connections (terminate with Ctrl-C) try: while True: connectionSocket, address = serverSocket.accept() print "Connected from", address sentence = Lib.readTextTCP(connectionSocket) print "Client beder om: ", sentence file_name = Lib.extractFilename(sentence) print "Filnavn: ", file_name file_size = Lib.check_File_Exists(sentence) if file_size != 0: print "Filen findes!" print "Filen fylder: ", file_size Lib.writeTextTCP(str(file_size), connectionSocket) sendFile(sentence, file_size, connectionSocket) print "Filen er sendt!" else: print "Kuk kuk - tom fil eller er den bare tom?" Lib.writeTextTCP("Ingen fil..", connectionSocket) connectionSocket.close() print "Disconnected from", address finally: serverSocket.close()
def main(argv): try: #Initiate server serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #INET equals streaming socket equals TCP #Prevent socket from blocking #serversocket.setblocking(0) serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #Bind socket to PORT and localhost serversocket.bind((HOST,PORT)) serversocket.listen(1) #We want to queue up to just one client print 'Socket connection on: ', serversocket.getsockname() except socket.error as msg: print 'Error connecting with serversocket: %s\n Terminating program.' %msg serversocket.close() sys.exit() while(1): #Wait for connections (clientsocket, address) = serversocket.accept() print 'Incomming connection from', address filename = Lib.readTextTCP(clientsocket) #Connect with client print '1 ', filename filesize = Lib.check_File_Exists(filename) #Returns size print '2 filesize ', filesize if filesize != 0: sendFile(filename, filesize, clientsocket) else: Lib.writeTextTCP("0", clientsocket) Lib.writeTextTCP("File " + filename + "doesnt exist.", clientsocket) print '3 Closing connection ', address clientsocket.close()
def main(argv): serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.bind((LOCALIP, PORT)) serverSocket.listen(1) print("The server is ready to recieve\n") while True: connectionSocket, addr = serverSocket.accept() print("client connected\n") msg = Lib.readTextTCP(connectionSocket) while msg != "exit": print("Command received: %s\n" % msg) fileSize = Lib.check_File_Exists(msg) if fileSize > 0: sendFile(msg, fileSize, connectionSocket) else: Lib.writeTextTCP(str(fileSize), connectionSocket) msg = Lib.readTextTCP(connectionSocket) connectionSocket.close() print("Connection closed")
def getTargetFileSize(targetFileName): targetFileSize = "" targetFileSize = Lib.check_File_Exists(Lib.extractFilename(targetFileName)) print('Size:') print(targetFileSize) return targetFileSize