def main(): print ("Server Starts") # Handling the argument arg = sys.argv if len(arg) != 3: print 'Invalid command. Please try again.' sys.exit() log = "output-server.txt" #pass the command line arguments try: hostPort = int(arg[1]) except ValueError: print 'Invalid command. Please try again.' sys.exit() # validate if not 0 < hostPort < 65536: print 'Invalid port number. Please try again.' sys.exit() #Server IP address serverIP = '127.0.0.1' # validate if not _validIP(serverIP): print 'IP address is not valid, please try again' sys.exit() window = arg[2] # connect and listen to the port rtpProtocol = rtp(serverIP, hostPort, 0, 0, None, False) serverProtocol = RecvThread(rtpProtocol) serverProtocol.start() rtpProtocol.setWindowSize(window) # execute user's commend while (True): Sinput = raw_input("close - to terminate the server\n") if Sinput.__eq__("close"): serverProtocol.stop() for thread in rtpProtocol.threads: thread.stop() print ("Server is closed") break
def main() : # check if no enough number of arguments if len(sys.argv) < 4 : print >> sys.stderr, 'illegal number of arguments' sys.exit() # check if input contains ':' to separate ip and port if ':' not in sys.argv[1] : print "use : to split address and port number" sys.exit() address = sys.argv[1].split(':', 1) # check if port number is not digit if not address[1].isdigit() : print "invaild port number" sys.exit() # setup ip address serverIP = address[0] if address[0] == "localhost" : serverIP = "127.0.0.1" # use tryNumber to count down number of try after not reciving response tryNumber = 3 # make up our query from arguments query = "(queryExecution) " for q in sys.argv[2::] : query += q + ' ' # set timeout for socket desPort = int(address[1]) hostAddress = '127.0.0.1' window = 2 rtpProtocol = rtp(hostAddress, 7778, serverIP, desPort, None, True) clientProtocol = RecvThread(rtpProtocol) clientProtocol.start() try: rtpProtocol.connect() except Exception, e: print e clientProtocol.stop() rtpProtocol.socket.close() rtpProtocol = None return
def main() : # handle illegal numer of arguments if len(sys.argv) != 2 : print >> sys.stderr, 'illegal number of arguments' sys.exit() port = sys.argv[1] # check if port number is not digit if not port.isdigit() : print >> sys.stderr, 'illegal port number' sys.exit() port = int(sys.argv[1]) serverIP = '127.0.0.1' # hard-code database window = 2 # Create a UDP socket rtpProtocol = rtp(serverIP, port, 0, 0, None, False) serverProtocol = RecvThread(rtpProtocol) serverProtocol.start() rtpProtocol.setWindowSize(window) while True: clientSocket = rtpProtocol.accept() try: data = clientSocket.recv() # if header of message is not "queryExecutionRequest", send error message if "queryExecution" not in data: clientSocket.sendAll("(queryExecution) unknown request. please send query execution request\n") continue # execute query and send back result print data[17::] result = execute(data[17::]) clientSocket.sendAll(result) except Exception, exc: print(traceback.format_exc())
def main(): rxpProtocol = None # Handling the argument arg = sys.argv if len(arg) < 3 or len(arg) > 4: print 'Invalid command. Please try again.' sys.exit() # pass the command line arguments try: clientPort = int(arg[1]) except ValueError: print 'Invalid command. Please try again.' sys.exit() # validate if not 0 < clientPort < 65536: print 'Invalid port number. Please try again.' sys.exit() # Server IP address serverIP = arg[2] if not _validIP(serverIP): print 'IP address is not valid, please try again' sys.exit() # netEmu port number try: netEmuPort = int(arg[3]) except ValueError: print 'Invalid command. Please try again.' sys.exit() # validate if not 0 < netEmuPort < 65536: print 'Invalid port number. Please try again.' sys.exit() # Dest. port number desPort = clientPort + 1 log = "output-client.txt" clientProtocol = None sendThread = None connThread = None sThread = None #execute user's commend while True: time.sleep(.500) Sinput = raw_input("type connect - to establish connection \n" + "get 'filename' - to download the file from server \n" + "post 'filename' - to upload the file to server \n" + "Window W - to change the window size \n" + "disconnect - to close the connection\n" + 'quit - to quit the application\n') if Sinput.__eq__("connect"): rxpProtocol = RxP(serverIP, netEmuPort, clientPort, desPort, log) clientProtocol = RecvThread(rxpProtocol) clientProtocol.start() rxpProtocol.connect() # get file form server elif "get" in Sinput: if rxpProtocol != None: s = Sinput.split() rxpProtocol.getFile(s[1]) # post file form server elif "post" in Sinput: if rxpProtocol != None: s = Sinput.split() sendThread = SendThread(rxpProtocol, s[1]) sendThread.start() # set the window size elif "window" in Sinput: if rxpProtocol != None: s = Sinput.split() try: window = int(s[1]) except ValueError: print 'Invalid window size. Please try again.' sys.exit() if not 0 < window < 50: print 'Window size too big. Please try again.' sys.exit() print "Set window size to " + str(window) rxpProtocol.setWindowSize(window) #close connection elif Sinput.__eq__("disconnect"): if rxpProtocol != None: rxpProtocol.close() clientProtocol.stop() rxpProtocol.socket.close() rxpProtocol = None elif Sinput.__eq__("quit"): if rxpProtocol: print 'disconnect before quit' else: break
def main(): print ("Server Starts") # Handling the argument arg = sys.argv if len(arg) < 3 or len(arg) > 4: print 'Invalid command. Please try again.' sys.exit() log = "output-server.txt" #pass the command line arguments try: hostPort = int(arg[1]) except ValueError: print 'Invalid command. Please try again.' sys.exit() # validate if not 0 < hostPort < 65536: print 'Invalid port number. Please try again.' sys.exit() #Server IP address serverIP = arg[2] # validate if not _validIP(serverIP): print 'IP address is not valid, please try again' sys.exit() # netEmu port number try: netEmuPort = int(arg[3]) except ValueError: print 'Invalid command. Please try again.' sys.exit() # validate if not 0 < netEmuPort < 65536: print 'Invalid port number. Please try again.' sys.exit() # Dest. port number desPort = hostPort - 1 rxpProtocol = RxP(serverIP, netEmuPort, hostPort, desPort, log) serverProtocol = RecvThread(rxpProtocol) serverProtocol.start() # execute user's commend while (True): Sinput = raw_input("type Window W - to change the window size \n" + "terminate - to terminate the server\n") if "window" in Sinput: s = Sinput.split() try: wsize = int(s[1]) except ValueError: print 'Invalid window size. Please try again.' sys.exit() if not 0 < wsize < 50: print 'Window size too big. Please try again.' sys.exit() print "Set window size to " + str(wsize) rxpProtocol.setWindowSize(wsize) # close server elif Sinput.__eq__("terminate"): rxpProtocol.close() serverProtocol.stop() for thread in rxpProtocol.threads: thread.stop() print ("Server is closed") break
def main(): # set default window size = 2 window = 2 rtpProtocol = None # Handling the argument arg = sys.argv if len(arg) != 3: print 'Invalid command. Please try again.' sys.exit() arg1 = arg[1].split(":") # Server IP address serverIP = arg1[0]; if not _validIP(serverIP): print 'IP address is not valid, please try again' sys.exit() # Dest. port number desPort = int(arg1[1]) if not 0 < desPort < 65536: print 'Invalid port number. Please try again.' sys.exit() window = int(arg[2]) clientProtocol = None sendThread = None connThread = None sThread = None hostAddress = '127.0.0.1' # connect rtpProtocol = rtp(hostAddress, 8889, serverIP, desPort, None, True) clientProtocol = RecvThread(rtpProtocol) clientProtocol.start() rtpProtocol.connect() rtpProtocol.setWindowSize(window) #execute user's commend while True: time.sleep(.500) Sinput = raw_input("get F - to download the file from server \n" + "post G - to upload the file to server \n" + "get-post F G - to download F and upload G at same time \n" + "disconnect - to close the connection\n") # get file and post file from server at a same time if "get-post" in Sinput: if rtpProtocol != None: s = Sinput.split(' ') sendThread = SendThread(rtpProtocol, s[2]) sendThread.start() rtpProtocol.getFile(s[1]) # get file from server elif "get" in Sinput: if rtpProtocol != None: s = Sinput.split() rtpProtocol.getFile(s[1]) # post file form server elif "post" in Sinput: if rtpProtocol != None: s = Sinput.split() sendThread = SendThread(rtpProtocol, s[1]) sendThread.start() #close connection elif Sinput.__eq__("disconnect"): if rtpProtocol != None: rtpProtocol.close() clientProtocol.stop() rtpProtocol.socket.close() rtpProtocol = None break