diff --git a/client.py b/client.py index 5b32044..725621a 100644 --- a/client.py +++ b/client.py @@ -128,7 +128,12 @@ def ftp_client(host, port, cert): context.check_hostname = False sock = context.wrap_socket(socket(AF_INET, SOCK_STREAM), server_hostname=host) - sock.connect((host, port)) + try: + sock.connect((host, port)) + print("Connected to " + host + ":" + str(port)) + except ConnectionRefusedError: + print("Connection refused by " + host + ":" + str(port)) + return response = sock.recv(BUFFER_SIZE).decode().strip() print(response) @@ -168,18 +173,28 @@ def ftp_client(host, port, cert): if __name__ == "__main__": - # FTP Client should be able to define IP and port + # Get command line arguments + try: + ip = sys.argv[1] + port = int(sys.argv[2]) + except IndexError: + pass + + # Config file config = configparser.ConfigParser() config.read('./config/config.ini') - ip = config['FTPSERVER']['ip'] - port = int(config['FTPSERVER']['port']) cert = config['SSL']['cert'] + # If no command line arguments, use config file + if len(sys.argv) == 1: + print("Using Config.ini") + # FTP Client should be able to define IP and port + ip = config['FTPSERVER']['ip'] + port = int(config['FTPSERVER']['port']) + + try: ftp_client(ip, port, cert) except KeyboardInterrupt: print("Client interrupted") - try: - sys.exit(0) - except SystemExit: - os._exit(0) + sys.exit(0) diff --git a/server.py b/server.py index 40c46df..a3cae65 100644 --- a/server.py +++ b/server.py @@ -171,7 +171,7 @@ def handle_connection(conn): command = args[0] print(f'Command: {command}') if command in commands: - if commands == handle_quit: + if command == 'QUIT': conn.sendall(b'Closing Connection') conn.close() break @@ -182,7 +182,7 @@ def handle_connection(conn): print('Connection closed by client') break except OSError as e: - print(e) + print("OS Error: {0}".format(e)) break # For FTP client testing @@ -194,19 +194,23 @@ def handle_connection(conn): def main(): - # FTP Server should be run with a port number as an argument - # if len(sys.argv) != 2: - # print("Usage: python3 server.py ") - # return + # Get command line arguments + try: + port = int(sys.argv[1]) + except IndexError: + pass config = configparser.ConfigParser() config.read('./config/config.ini') - # TODO: Need to allow user to define port through command line as well - port = int(config['FTPSERVER']['port']) + # SSL certificate and key cert = config['SSL']['cert'] key = config['SSL']['key'] + # If port is not specified, use default port + if len(sys.argv) < 2: + port = int(config['FTPSERVER']['port']) + # print(cert, key) context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)