try: with open(fileName, 'r') as fl: allLines = fl.readlines() if len(allLines) == 0: print("file's empty....exiting") sys.exit(0) except FileNotFoundError: print("no such file or directory") sys.exit(1) ############################################ if s is None: print('could not open socket') sys.exit(1) print("sending file name: '%s'" % fileName) sendAll(s, fileName.encode()) data = s.recv(1024).decode() print("Received '%s'" % data) print("*************sending file contents!*****************") for i in allLines: i = i.encode() sendAll(s,i) #print("file line:%s"%i) #sendAll(s, allLines) s.shutdown(socket.SHUT_WR) # no more output while 1: data = s.recv(1024).decode()
if paramMap['usage']: params.usage() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((listenAddr, listenPort)) s.listen(1) # allow only one outstanding request # s is a factory for connected sockets conn, addr = s.accept( ) # wait until incoming connection request (and accept it) print('Connected by', addr) fileName = conn.recv(1024).decode() print("filename is ", fileName) conf = "filename received and stored" sendAll(conn, conf.encode()) path = os.getcwd() filesPath = path + '\\' + fileName print(filesPath) if (os.path.isfile(filesPath)): print("file already exists in server....exiting") conn.close() sys.exit(0) f = open(fileName, "w") while 1: data = conn.recv(1024).decode() f.write(data) if not data: break sendMsg = f"Echoing <{data}>" #print(f"Received <{data}>, sending <{sendMsg}>") sendAll(conn, sendMsg.encode())
s = socket.socket(addrFamily, socktype) if s is None: print('could not open socket') sys.exit(1) s.connect(addrPort) if s is None: print('could not open socket') sys.exit(1) outMessage = b"Hello world!" print("sending '%s'" % outMessage) sendAll(s, outMessage) data = s.recv(1024).decode() print("Received '%s'" % data) print("sending '%s'" % outMessage) sendAll(s, outMessage) s.shutdown(socket.SHUT_WR) # no more output while 1: data = s.recv(1024).decode() print("Received '%s'" % data) if len(data) == 0: break print("Zero length read. Closing")