def main(records_file=None): # Listen on a specified port... server_sock = library.CreateServerSocket(LISTENING_PORT) if records_file: try: database = library.KeyValueStore(fileName=records_file, isTimer=True) except library.InvalidRecordFormatException as e: print(e) print("Initializing an empty cache.") database = library.KeyValueStore() except library.InvalidRecordTypeException as e: print(e) print("Initializing an empty cache.") database = library.KeyValueStore() else: cache = library.KeyValueStore(isTimer=True) # Accept incoming commands indefinitely. try: while True: # Wait until a client connects and then get a socket that connects to the # client. client_sock, (address, port) = library.ConnectClientToServer(server_sock) print('Received connection from %s:%d' % (address, port)) ProxyClientCommand(client_sock, SERVER_ADDRESS, SERVER_PORT, cache) client_sock.close() except KeyboardInterrupt: # Close server socket. # Write the records to a file for later use. server_sock.close() with open("proxy-records.txt", 'w') as fileHandle: fileHandle.write(str(cache))
def main(records_file=None): # Store all key/value pairs in here. if records_file: try: database = library.KeyValueStore(fileName=records_file) except library.InvalidRecordFormatException as e: print(e) print("Initializing an empty database") database = library.KeyValueStore() except library.InvalidRecordTypeException as e: print(e) print("Initializing an empty database") database = library.KeyValueStore() else: database = library.KeyValueStore() # The server socket that will listen on the specified port. If you don't # have permission to listen on the port, try a higher numbered port. server_sock = library.CreateServerSocket(LISTENING_PORT) # Handle commands indefinitely. Use ^C to exit the program. try: while True: # Wait until a client connects and then get a socket that connects to the # client. client_sock, (address, port) = library.ConnectClientToServer(server_sock) print('Received connection from %s:%d' % (address, port)) # Read a command. command_line = library.ReadCommand(client_sock) command, name, text = library.ParseCommand(command_line) # Execute the command based on the first word in the command line. if command == 'PUT': result = PutCommand(name, text, database) elif command == 'GET': result = GetCommand(name, database) elif command == 'DUMP': result = DumpCommand(database) else: SendText(client_sock, 'Unknown command %s' % command) SendText(client_sock, result) # We're done with the client, so clean up the socket. client_sock.close() except KeyboardInterrupt: # Write records to a file, which can be restored later. # Close the server socket. server_sock.close() with open("server-records.txt", 'w') as fileHandle: fileHandle.write(str(database))
def main(): # Store all key/value pairs in here. database = library.KeyValueStore() # The server socket that will listen on the specified port. If you don't # have permission to listen on the port, try a higher numbered port. server_sock = library.CreateServerSocket(LISTENING_PORT) # Handle commands indefinitely. Use ^C to exit the program. while True: # Wait until a client connects and then get a socket that connects to the # client. client_sock, (address, port) = library.ConnectClientToServer(server_sock) print('Received connection from %s:%d' % (address, port)) # Read a command. command_line = library.ReadCommand(client_sock) command, name, text = library.ParseCommand(command_line) # Execute the command based on the first word in the command line. if command == 'PUT': result = PutCommand(name, text, database) elif command == 'GET': result = GetCommand(name, database) elif command == 'DUMP': result = DumpCommand(database) else: result = 'Unknown command %s' % command SendText(client_sock, result) client_sock.close()
def main(): # Store all key/value pairs in here. database = library.KeyValueStore() # The server socket that will listen on the specified port. If you don't # have permission to listen on the port, try a higher numbered port. server_sock = library.CreateServerSocket(LISTENING_PORT) # It will now handle it completely. while True: # Wait until a client connects and then get a socket that connects to the # client. client_sock, (address, port) = library.ConnectClientToServer( server_sock, LISTENING_PORT) print('Received connection from %s:%d' % (address, port)) # It now reads the command and executes it. command_line = library.ReadCommand(client_sock) command, name, text = library.ParseCommand(command_line) if command == 'PUT': result = PutCommand(name, text, database) elif command == 'GET': result = GetCommand(name, database) elif command == 'DUMP': result = DumpCommand(database) else: SendText(client_sock, 'Unknown command %s' % command) SendText(client_sock, result) # We're done with the client, so clean up the socket. ################################# client_sock.close()
def main(): # Listen on a specified port... server_sock = library.CreateServerSocket(LISTENING_PORT) cache = library.KeyValueStore() # Accept incoming commands indefinitely. while True: # Wait until a client connects and then get a socket that connects to the # client. client_sock, (address, port) = library.ConnectClientToServer(server_sock) print('Received connection from %s:%d' % (address, port)) ProxyClientCommand(client_sock, SERVER_ADDRESS, SERVER_PORT, cache)
def main(): # Listen on a specified port server_socket = library.create_server_socket(LISTENING_PORT) cache = library.KeyValueStore() # Accept incoming commands indefinitely while True: # Wait until a client connects and then get a socket that connects to the # client. # Establish the connection client_socket, (address, port) = library.connect_client_to_server(server_socket) print('Received connection from %s:%d' % (address, port)) # Redirect traffic proxy_client_command(client_socket, SERVER_ADDRESS, SERVER_PORT, cache) client_socket.close()
def main(): # Store all key/value pairs in here. database = library.KeyValueStore() # The server socket that will listen on the specified port. If you don't # have permission to listen on the port, try a higher numbered port. server_sock = library.create_server_socket(LISTENING_PORT) # Handle commands indefinitely. Use ^C to exit the program. while True: # Wait until a client connects and then get a socket that connects to the # client. client_s, (address, port) = library.connect_client_to_server(server_sock) print('Received connection from %s:%d' % (address, port)) # Read a command. command_line = library.read_command(client_s) command, name, text = library.parse_command(command_line) # Execute the command based on the first word in the command line. if command == 'PUT': result = put_command(name, text, database) elif command == 'GET': result = get_command(name, database) elif command == 'DUMP': result = dump_command(database) else: send_text(client_s, 'Unknown command %s' % command) send_text(client_s, result) # We're done with the client, so the socket closes. client_s.close()