Modified both client and server to take in args from cli, fixed bad file descriptor error
This commit is contained in:
parent
9cb37654fc
commit
f843bc3440
25
client.py
25
client.py
|
@ -128,7 +128,12 @@ def ftp_client(host, port, cert):
|
||||||
context.check_hostname = False
|
context.check_hostname = False
|
||||||
|
|
||||||
sock = context.wrap_socket(socket(AF_INET, SOCK_STREAM), server_hostname=host)
|
sock = context.wrap_socket(socket(AF_INET, SOCK_STREAM), server_hostname=host)
|
||||||
|
try:
|
||||||
sock.connect((host, port))
|
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()
|
response = sock.recv(BUFFER_SIZE).decode().strip()
|
||||||
print(response)
|
print(response)
|
||||||
|
@ -168,18 +173,28 @@ def ftp_client(host, port, cert):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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 = configparser.ConfigParser()
|
||||||
config.read('./config/config.ini')
|
config.read('./config/config.ini')
|
||||||
|
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']
|
ip = config['FTPSERVER']['ip']
|
||||||
port = int(config['FTPSERVER']['port'])
|
port = int(config['FTPSERVER']['port'])
|
||||||
cert = config['SSL']['cert']
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ftp_client(ip, port, cert)
|
ftp_client(ip, port, cert)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Client interrupted")
|
print("Client interrupted")
|
||||||
try:
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
except SystemExit:
|
|
||||||
os._exit(0)
|
|
||||||
|
|
20
server.py
20
server.py
|
@ -171,7 +171,7 @@ def handle_connection(conn):
|
||||||
command = args[0]
|
command = args[0]
|
||||||
print(f'Command: {command}')
|
print(f'Command: {command}')
|
||||||
if command in commands:
|
if command in commands:
|
||||||
if commands == handle_quit:
|
if command == 'QUIT':
|
||||||
conn.sendall(b'Closing Connection')
|
conn.sendall(b'Closing Connection')
|
||||||
conn.close()
|
conn.close()
|
||||||
break
|
break
|
||||||
|
@ -182,7 +182,7 @@ def handle_connection(conn):
|
||||||
print('Connection closed by client')
|
print('Connection closed by client')
|
||||||
break
|
break
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print(e)
|
print("OS Error: {0}".format(e))
|
||||||
break
|
break
|
||||||
|
|
||||||
# For FTP client testing
|
# For FTP client testing
|
||||||
|
@ -194,19 +194,23 @@ def handle_connection(conn):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# FTP Server should be run with a port number as an argument
|
# Get command line arguments
|
||||||
# if len(sys.argv) != 2:
|
try:
|
||||||
# print("Usage: python3 server.py <port>")
|
port = int(sys.argv[1])
|
||||||
# return
|
except IndexError:
|
||||||
|
pass
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read('./config/config.ini')
|
config.read('./config/config.ini')
|
||||||
|
|
||||||
# TODO: Need to allow user to define port through command line as well
|
# SSL certificate and key
|
||||||
port = int(config['FTPSERVER']['port'])
|
|
||||||
cert = config['SSL']['cert']
|
cert = config['SSL']['cert']
|
||||||
key = config['SSL']['key']
|
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)
|
# print(cert, key)
|
||||||
|
|
||||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||||
|
|
Loading…
Reference in New Issue