feat: Testing environment

- both server and client must specify their directories
This commit is contained in:
2023-11-27 14:19:19 -05:00
parent fa544ac1fb
commit d1d5321784
7 changed files with 130 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
from socket import socket, AF_INET, SOCK_DGRAM
from typing import Tuple
from argparse import ArgumentParser
import os
# custome type to represent the hostname(server name) and the server port
Address = Tuple[str, int]
@@ -19,12 +20,13 @@ class UDPClient:
if not self.pong_received:
return
client_socket = socket(AF_INET, SOCK_DGRAM)
client_socket.connect((self.server_name, self.server_port))
client_socket = None # shutup the variableNotBound warning
while True:
try:
client_socket = socket(AF_INET, SOCK_DGRAM)
client_socket.connect((self.server_name, self.server_port))
# get command from user
while (command := input(f"myftp> - {self.mode} - : ")) not in [
"put",
@@ -117,6 +119,20 @@ def get_address_input() -> Address:
)
def check_directory(path):
if os.path.exists(path):
if os.path.isdir(path):
if os.access(path, os.R_OK) and os.access(path, os.W_OK):
return True
else:
print(f"Error: The directory '{path}' is not readable or writable.")
else:
print(f"Error: '{path}' is not a directory.")
else:
print(f"Error: The directory '{path}' does not exist.")
return False
def init():
arg_parser = ArgumentParser(description="A FTP client written in Python")
@@ -129,6 +145,10 @@ def init():
help="Enable or disable the flag (0 or 1)",
)
arg_parser.add_argument(
"--directory", required=True, type=str, help="Path to the client directory"
)
args = arg_parser.parse_args()
while (
@@ -136,6 +156,12 @@ def init():
) not in {"1", "2"}:
print("myftp>Invalid choice. Press 1 for TCP, Press 2 for UDP")
if not check_directory(args.directory):
print(
f"The directory '{args.directory}' does not exists or is not readable/writable."
)
return
# UDP client selected here
if protocol_selection == "2":
user_supplied_address = get_address_input()

View File

@@ -1,5 +1,6 @@
from socket import socket, AF_INET, SOCK_DGRAM
from argparse import ArgumentParser
import os
class UDPServer:
@@ -48,6 +49,20 @@ class UDPServer:
print(f"myftp> - {self.mode} - Closed the server socket\n")
def check_directory(path):
if os.path.exists(path):
if os.path.isdir(path):
if os.access(path, os.R_OK) and os.access(path, os.W_OK):
return True
else:
print(f"Error: The directory '{path}' is not readable or writable.")
else:
print(f"Error: '{path}' is not a directory.")
else:
print(f"Error: The directory '{path}' does not exist.")
return False
def init():
parser = ArgumentParser(description="A FTP server written in Python")
@@ -59,6 +74,10 @@ def init():
help="Port number for the server. Default = 12000",
)
parser.add_argument(
"--directory", required=True, type=str, help="Path to the server directory"
)
parser.add_argument(
"--ip_addr",
default="0.0.0.0",
@@ -82,6 +101,12 @@ def init():
) not in {"1", "2"}:
print("myftp>Invalid choice. Press 1 for TCP, Press 2 for UDP")
if not check_directory(args.directory):
print(
f"The directory '{args.directory}' does not exists or is not readable/writable."
)
return
# UDP client selected here
if protocol_selection == "2":
udp_server = UDPServer(args.ip_addr, args.port_number, args.debug)