class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) filename = self.fsock.receive(debug) ## GET FILENAME ## ACQUIRE filelock.acquire() FileTransferStart( filename) ## Check if thread is using filename already filelock.release() newfile = filename.decode() newfile = "new" + newfile if exists(newfile): self.fsock.send(b"True", debug) else: self.fsock.send(b"False", debug) payload = self.fsock.receive(debug) if debug: print("rec'd: ", payload) if not payload: if debug: print(f"thread connected to {addr} done") self.fsock.close() return outfile = open(newfile, "wb") outfile.write(filename) outfile.write(payload) FileTransferEnd(filename) ## Thread done with file self.fsock.send(b"file saved to server", debug)
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("Thread connection from", self.addr) filename = self.fsock.receive(debug) lock.aquire() fileStart(filename) lock.release() file = filename.decode() file = "new" + flle if exists(file): self.fsock.send(b"True", debug) else: self.fsock.send(b"False", debug) payload = self.fsock.receive(debug) if debug: print("rec'd; : ", payload) if not payload: if debug: print(f"thread connected to {addr} ") self.fsock.close() return outputFile = open(file, "wb") outputFile.write(filename) outputFile.write(payload) fileEnd(filename) self.fsock.send(b" Thread done", debug)
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) serverFile = self.fsock.receive(debug) fileLock.acquire() fileTransfer(serverFile) fileLock.release() nfile = serverFile.decode() nfile = "transfered"+nfile if exists(nfile): self.fsock.send(b"True",debug) else: self.fsock.send(b"False", debug) payload = self.fsock.receive(debug) if debug: print("rec'd: ",payload) if not payload: if debug: print(f"thread connected to {addr} done") self.fsock.close() return outfile = open(nfile,"wb") outfile.write(serverFile) outfile.write(payload) fileDisconnect(serverFile) ## Thread done with file self.fsock.send(b"file saved to server",debug)
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print('Connected by', self.addr) file_info = self.fsock.receive(debug).decode("utf-8").split(":") #First receives filename and file size from client. filename, file_size_str = file_info[0], file_info[1] print("File receiving: '" + filename + "' Size: " + file_size_str) file_size = int(file_size_str) if os.path.exists("received_files/" + filename) == True: #Checks if filename is in the directory self.fsock.send(b'1', debug) #Sends back to client that file exists on server directory print("File exists. Overwriting original...") else: self.fsock.send(b'0', debug) #Sends back to client that file does not exist on server directory lock.acquire() #Gets lock to check active files if filename in active_files: #If filename is in the list, then it will exit out. print("Cannot complete transfer. File is active.") self.fsock.close() lock.release() return else: #Adds file name to active list active_files.append(filename) lock.release() #Releases lock to resume normal function with open("received_files/" + filename, 'wb') as file: #Creates file to write data to while 1: data = self.fsock.receive(debug) #Receives data from client if debug: #Debug info print("Data received: ", data) if not data: #Exits if data is None type, non-existent, or end of file if file_size == os.path.getsize("received_files/" + filename): print("File: '" + filename + "' received!") else: print("File is incomplete due to a dropped connection. Transfer incomplete!") lock.acquire() active_files.remove(filename) #Removes active file(name) from list within lock lock.release() if debug: print(f"thread connected to {addr} done") self.fsock.close() #Close connection file.close() return file.write(data) #Writes to file self.fsock.send(data, debug) #Sends response back to client
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): global dictionary, dictLock print("new thread handling connection from", self.addr) payload = self.fsock.receive(debug) #receive file name to be saved if debug: print("rec'd: ", payload) if not payload: # done if debug: print(f"thread connected to {addr} done") self.fsock.close() #possible error return # exit payload = payload.decode( ) #receive byte array of name to be saved and convert to string if exists(payload): self.fsock.send(b"True", debug) else: dictLock.acquire( ) #Acquire lock to check if the wanted file is not already being saved by another thread currentCheck = dictionary.get(payload) if currentCheck == 'running': #Checking dictionary self.fsock.send(b"True", debug) dictLock.release() print("the file " + payload + " is currently being transfered") else: dictionary[ payload] = "running" #If it is not currently being transferred then the thread can transfer it and dictLock.release( ) #you write to the dictionary that you are transferring the file sleep(40) self.fsock.send( b"False", debug ) #Tell client that the file is not being transferred and does not exist in the try: #directory payload2 = self.fsock.receive(debug) #Receiving file data except: print("connection lost while receiving.") sys.exit(0) if not payload2: sys.exit(0) try: self.fsock.send(payload2, debug) except: print("------------------------------") print("connection lost while sending.") print("------------------------------") output = open(payload, 'wb') #open and set to write byte array output.write(payload2) #writing to file output.close() dictLock.acquire( ) #After the file has been written, delete it from the dictionary del dictionary[payload] dictLock.release() self.fsock.close()
class Server(Thread): def __init__(self, sockAddress): Thread.__init__(self) self.sock, self.address = sockAddress self.fsock = EncapFramedSock(sockAddress) def run(self): print("new thread handling connection from", self.address) while True: try: fileName, contents = self.fsock.receive(debug) except: print("Error: File transfer was not successful!") self.fsock.sendStatus(0, debug) self.fsock.close() sys.exit(1) if debug: print("Received", contents) # Data not received. Exit system. if fileName is None or contents is None: print("Client ", self.address, " has disconnected") sys.exit(0) lock.acquire() if debug: time.sleep(5) fileName = fileName.decode() self.writeFile(fileName, contents) self.fsock.sendStatus(1, debug) lock.release() def writeFile(self, fileName, contents): if fileName is None: raise TypeError if contents is None: raise TypeError try: # Check if there is a directory to receive files. if not os.path.exists(PATH): os.makedirs(PATH) os.chdir(PATH) # New file to open. writer = open(fileName, 'w+b') writer.write(contents) writer.close() print("File %s received from %s" % (fileName, self.address) ) # Prints output to ensure the file was received. except FileNotFoundError: print("File Not Found Error: File %s not found" % fileName) # File not found error. Exit. self.fsock.Status(0, debug) sys.exit(1)
class Server(Thread): def __init__(self, sockAddress): Thread.__init__(self) self.sock, self.address = sockAddress self.fsock = EncapFramedSock(sockAddress) def run(self): print("new thread handling connection from", self.address) while 1: try: print("try statement 1 ********") fileName, contents = self.fsock.receive() print("done **********") except: print("Error: File transfer was not successful!") self.fsock.sendStatus(0) self.fsock.close() sys.exit(1) # data not received if fileName is None or contents is None: print ("Client ", self.address, " has disconnected") sys.exit(0) print("writing *********") lock.acquire() # write the file fileName = fileName.decode() self.writeFile(fileName, contents) self.fsock.sendStatus(1) lock.release() print("release *********") def writeFile(self, fileName, contents): if fileName is None: raise TypeError if contents is None: raise TypeError try: # check if dir exists to receive files if not os.path.exists(PATH): os.makedirs(PATH) os.chdir(PATH) # create file to write writer = open(fileName, 'w+b') writer.write(contents) writer.close() print("File %s received from %s" % (fileName, self.address)) except FileNotFoundError: print("File Not Found Error: File %s not found" % fileName) self.fsock.Status(0) sys.exit(1)
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) while True: payload = self.fsock.receive(debug) if debug: print("rec'd: ", payload) if not payload: # done if debug: print(f"thread connected to {addr} done") self.fsock.close() return # exit while 1: sock, addr = lsock.accept() print("Connection from", addr) while working: payloadFileName = framedReceive(sock, debug) if not payloadFileName: working = False break lock.aquire() payloadFileName = payloadFileName.decode() if exists(payloadFileName): framedSend(sock, b"True",1) else: framedSend(sock, b"False",1) try: payloadTxt = framedReceive(sock, debug) except: print("Connection with ",addr ,"lost ") sys.exit(0) lock.release() try: framedSend(sock,b"Message Completed!",debug) except: print("Connection with ",addr ,"lost ") sys.exit(0) lock.release() output = open(payloadFileName, 'a') payloadTxt = payloadTxt.decode() output.write(payloadTxt) output.close() lock.release()
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) while True: lock.acquire() ### from framedSock import framedSend, framedReceive ################################################# fileName = self.fsock.receive(debug).decode() print("filename is ", fileName) conf = "filename received and stored" #framedSend(sock, conf.encode(), debug) self.fsock.send(conf.encode(), debug) path = os.getcwd() #/ filesPath = path + '/' + fileName print(filesPath) if (os.path.isfile(filesPath)): print("file already exists in server....exiting") #framedSend(sock, b"file exists", debug) self.fsock.send(b"file exists", debug) sys.exit(0) self.fsock.send(b"pass", debug) f = open(fileName, "w") ################################################ print("starting to write file....") while True: payload = self.fsock.receive(debug).decode() if debug: print("rec'd: ", payload) if payload == "end of file": closing = "end of file reached!" #framedSend(sock, closing.encode(), debug) self.fsock.send(closing.encode(), debug) f.close() lock.release() self.fsock.close() break f.write(payload) if not payload: self.fsock.close() return payload += "!" # make emphatic! self.fsock.send(payload.encode(), debug) break #for child
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def writeFile(self, fName): filesInUse.update( {fName:'True'} ) #mark as now in use, do not touch while in use lock.acquire() with open(fName, 'rb') as fContext: readData = fContext.read() with open(DIR+fName, 'wb') as wFile: wFile.write(readData) lock.release() filesInUse.update( {fName:'False'} ) #make open for use def run(self): print("new thread handling connection from", self.addr) while True: #Recieve payload = self.fsock.receive(debug) if debug: print(f"thread connected to ((addr var goes here!))done") if not payload: # done self.fsock.close() return # exit #Decode and echo status = payload payload = payload.decode() #exit if 'exit' in payload: sys.exit(0) #write payload if os.path.exists(payload): #Check to see if in use if filesInUse.get(payload) == None or filesInUse.get(payload) == 'False': #pass in the file name self.writeFile(payload) #Sent self.fsock.send(b'DONE', debug) print('Done writing to: ', payload , '\n') else: self.fsock.send(b'File In use, try again', debug) else: self.fsock.send(b'File does not exist, try again', debug)
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): # time.sleep(4) print("\nNew thread handling connection from", self.addr) # lock the critical section for threads lock.acquire() # get file sent by client payload = self.fsock.receive(debug) if debug: print("rec'd: ", payload) # if nothing is sent, release the lock if not payload: print("Client tried to send an empty file or an unexistent file") # close the socket self.fsock.close() lock.release() sys.exit(1) name = payload.decode() try: # if it doesnt exist in server, create the file and write its contents to it if not os.path.isfile(name): # create a file and write the content of the sent file to it content = self.fsock.receive(debug) file = open(name, 'wb+') file.write(content) file.close() print("Received file", name) # send info back to the client self.fsock.send(b"File has been received by the server", debug) else: print("The File with name", name, "already exists on server.") # send info back to the client self.fsock.send(b"File already exists on th server", debug) except FileNotFoundError: print("File wasnt found") lock.release() self.fsock.close()
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) while True: payload = self.fsock.receive(debug) if debug: print("rec'd: ", payload) if not payload: #connection done if debug: print(f"thread connected to %s done" % self.addr) self.fsock.close() return payload = payload.decode() lock.acquire() if debug: time.sleep(5) if exists(payload): self.fsock.send(b"True", debug) else: self.fsock.send(b"False", debug) try: payload2 = self.fsock.receive(debug) except: print("connection lost while receiving") sys.exit(0) if not payload2: break payload2 += b"!" try: self.fsock.send(payload2, debug) except: print("connection lost while sending") output = open(payload, 'wb') output.write(payload2) output.close() self.fsock.close() lock.release()
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) while True: payload = self.fsock.receive(debug) if debug: print("rec'd: ", payload) if not payload: # done if debug: print(f"thread connected to {addr} done") self.fsock.close() return # exit payload += b"!" # make emphatic! self.fsock.send(payload, debug)
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) while True: lock.acquire() filename = self.fsock.receive(debug) if debug: print("rec'd: ", filename) if filename is None: # done if debug: print("thread connected to {addr} done") #removed f here self.fsock.close() lock.release() return filename = filename.decode() if filename in files_being_transfered: self.fsock.send("True", debug) #remove b else: self.fsock.send("False", debug) #remove b files_being_transfered.append(filename) try: data = self.fsock.receive(debug) except: print( "Unable to receive and finalize transfer... terminating transfer..." ) sys.exit(0) open_file = open(filename, 'wb') open_file.write(data) open_file.close() self.sock.shutdown(socket.SHUT_RD) self.sock.close() lock.release()
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): global dictionary, dictLock print("new thread handling connection from", self.addr) while True: payload = self.fsock.receive() if not payload: self.fsock.close() return payload = payload.decode() if exists(payload): self.fsock.send(b"True") else: dictLock.acquire() currentCheck = dictionary.get(payload) if currentCheck == 'running': self.fsock.send(b"True") dictLock.release() else: dictionary[payload] = "running" dictLock.release() self.fsock.send(b"False") payload2 = self.fsock.receive() if not payload2: sys.exit(0) self.fsock.send(payload2) output = open(payload, 'wb') output.write(payload2) output.close() dictLock.acquire() del dictionary[payload] dictLock.release() self.fsock.close()
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) while True: payload = self.fsock.receive(debug) if debug: print("rec'd: ", payload) if not payload: if debug: print(f"thread connected to {addr} done") self.fsock.close() return # exit payload = payload.decode() transferStart(payload) output = open(payload, 'wb') output.write(payload) output.close() transferEnd(payload) self.fsock.send(payload, debug)
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): global dictionary, dictLock print("new thread handling connection from", self.addr) #get file name for transferFile fileName = self.fsock.receive(debug) #if debug: print("rec'd: ", fileName) #if not fileName: # done # if debug: print(f"thread connected to {addr} done") # self.fsock.close() #possible error # return # exit #convert transferFile name to string fileName = fileName.decode( ) #receive byte array of name to be saved and convert to string #check if file exists and send response if exists(fileName): self.fsock.send(b"True", debug) else: #lock dictLock.acquire() currentCheck = dictionary.get(fileName) #Check dictionary if currentCheck == 'running': self.fsock.send(b"True", debug) dictLock.release() print(fileName + "is currently being transfered") #continue if file doesn't exist else: #not being transferred dictionary[fileName] = "running" #release lock dictLock.release() sleep(10) self.fsock.send(b"False", debug) try: #receive file fileContents = self.fsock.receive(debug) except: print("error while receiving.") sys.exit(0) try: #send response self.fsock.send(fileContents, debug) except: print('error while sending!') #write new filename newFile = open(fileName, 'wb') #open and set to write byte array #write contents to file newFile.write(fileContents) #writing to file #close file newFile.close() dictLock.acquire() #delete file from dictionary del dictionary[fileName] dictLock.release() self.fsock.close()
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): print("new thread handling connection from", self.addr) while True: payload = self.fsock.receive(debug) if debug: print("rec'd: ", payload) if not payload: # done if debug: print(f"thread connected to {addr} done") self.fsock.close() return # exit simpleVar = payload.decode() try: self.fsock.send(payload, debug) except: print("Connection was cut short try again") inputVar = input( "Would you like to continue recieving anyways?: yes/no") if inputVar == "yes": continue else: print("Now exiting") sys.exit(0) output_file = simpleVar #aquire lock #Check to see # if file is in dictionary if output_file in lister: #Write to the client failure l.acquire() payload += b"FAILED CURRENTLY BEING USED!!!FAILED CURRENTLY BEING USED!!!" self.fsock.send(payload, debug) self.fsock.send(b"False", debug) l.release() exit else: l.acquire() lister.append(output_file) l.release() #release lock #exit #Else #Add to dictionary #release lock if (output_file): payload = self.fsock.receive(debug) output = open(output_file, 'wb') output.write(payload) #self.fsock.send(payload, debug) else: payload = self.fsock.receive(debug) output = open(output_file, 'wb') output.write(payload) try: self.fsock.send(payload, debug) except: print("connection lost while sending.") print("connection lost while sending.") print("connection lost while sending.") output.close() #Delete dictionary l.acquire() del lister[payload] l.release()
sys.exit(1) addrFamily = socket.AF_INET socktype = socket.SOCK_STREAM addrPort = (serverHost, serverPort) s = socket.socket(addrFamily, socktype) if s is None: print('could not open socket') sys.exit(1) s.connect(addrPort) encapSock = EncapFramedSock((s, addrPort)) # needs to be the file we read from fileName = input("File you want to send: ") # print(fileName, "This is test") if os.path.exists(fileName): # if file exists on client end inputFile = open(fileName, mode="r", encoding="utf-8") fileStuff = inputFile.read() if len(fileStuff) == 0: print('will not send empty file exiting') sys.exit(0) # don't send an empty file print("sending file") encapSock.send(fileName, fileStuff, debug=1) encapSock.close() # close socket after sending file
addrPort = (serverHost, serverPort) #connection begins sock = socket.socket(addrFamily, socktype) if sock is None: print('could not open socket') sys.exit(1) #if we connect successfully sock.connect(addrPort) #create encapFramedSock using our socket and port fsock = EncapFramedSock((sock, addrPort)) #get the file name, attached should be the extension(.txt, .pdf, etc...) name = input("What file do you want to send?") if not path.exists(name): print("That file does not exists..!") sys.exit(1) #check the file size is not 0, if it is handle error by exit if os.path.getsize(name) == 0: print("file size 0") sys.exit(1) #send to the server, print and close when done #fsock.sendName(name) fsock.send(name, debug) print("done sending..!") fsock.close()
class Server(Thread): def __init__(self, sockAddr): Thread.__init__(self) self.sock, self.addr = sockAddr self.fsock = EncapFramedSock(sockAddr) def run(self): global dictionary, dictLock print("new thread handling connection from", self.addr) while True: payload = self.fsock.receive(debug) #recieve file name to be saved if debug: print("rec'd: ", payload) if not payload: # done if debug: print(f"thread connected to {addr} done") self.fsock.close() #possible error return # exit payload = payload.decode( ) #recieve byte array and convert to string #self.fsock.send(payload, debug) if exists(payload): self.fsock.send(b"True", debug) else: print("1") dictLock.acquire() print('2') #payloadString = str(payload) currentCheck = dictionary.get(payload) print(dictionary) if currentCheck == 'running': self.fsock.send(b"True", debug) print('3') dictLock.release() print('4') print( "the file is currently being transfered, exiting now") else: print('5') dictionary[payload] = "running" dictLock.release() sleep(100) print('6') self.fsock.send(b"False", debug) #recieving file data try: payload2 = self.fsock.receive(debug) except: print("connection lost while recieving.") sys.exit(0) if not payload2: break payload2 += b"!" # make emphatic! try: self.fsock.send(payload2, debug) except: print("------------------------------") print("connection lost while sending.") print("------------------------------") output = open(payload, 'wb') #open and set to write byte array output.write(payload2) #writing to file print('7') dictLock.acquire() del dictionary[payload] print('8') dictLock.release() print('9') output.close() self.fsock.close()