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()
Example #2
0
def main(serverAddr, serverPort):
    server_socket = library.CreateServerSocket(serverAddr, serverPort)
    clientThreads = []

    # Handle commands indefinitely (^C to exit)
    while True:
        # Wait until a client connects, then get a socket for the  client.
        client_socket, addr = library.ConnectClientToServer(server_socket)

        # Read the request.
        request = library.ReadRequest(client_socket)
        command, filepath = library.ParseRequest(request)

        if command == 'GET' and filepath is not None:
            newClientThread = ClientThread(client_socket, filepath)
            newClientThread.start()
            clientThreads.append(newClientThread)

        else:
            client_socket.send(aes.encrypt(b't'))
            client_socket.send(aes.encrypt(b'Invalid request!\n'))
            client_socket.close()

    for t in clientThreads:
        t.join()

    server_socket.close()
Example #3
0
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():
    # 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)
Example #6
0
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))
Example #7
0
def main(conn, addr):
    s = library.CreateServerSocket(conn, addr)
    clientThreads = []

    # Handle commands indefinitely (^C to exit)
    while True:
        # Wait until a client connects, then get a socket for the  client.
        conn, addr = s.accept()

        newClientThread = ClientThread(conn, addr)
        newClientThread.ServeClient()
        clientThreads.append(newClientThread)

    for t in clientThreads:
        t.join()

    s.close()
Example #8
0
import sys
import library

HOST = 'fd5f:df5:bf3f:0:a75f:3554:2488:6f99'  # Symbolic name meaning all available interfaces
PORT = 8888  # Arbitrary non-privileged port

COMMAND_BUFFER_SIZE = 256


def ServeClient(conn, addr):
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data: break
            try:
                f = open(data.decode(), 'r')
                contents = f.read()
                f.close()
                conn.send(contents.encode())

            except FileNotFoundError:
                conn.send("File Not Found\n".encode())
                # Keep preset values


s = library.CreateServerSocket(HOST, PORT)
conn, addr = s.accept()
ServeClient(conn, addr)
s.close()