From 1eb4e072caa7df1d2986dc6ad56a063fd7a5f1ad Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Wed, 29 Nov 2023 00:00:51 -0500 Subject: [PATCH] feat(UDP): commands pattern matching - Added comments --- README.md | 2 +- src/myftp/client.py | 62 +++++++++++++++++++++++++++++++++++---------- src/myftp/server.py | 4 ++- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ddc3bc7..49f1f74 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Dependencies -Zero. Only python standard libs were used. +Zero. Only python standard libs were used. Tested on Python 3.11 ## Running diff --git a/src/myftp/client.py b/src/myftp/client.py index b6799c7..933f45c 100644 --- a/src/myftp/client.py +++ b/src/myftp/client.py @@ -1,8 +1,16 @@ from socket import socket, AF_INET, SOCK_DGRAM -from typing import Tuple +from typing import Pattern, Tuple from argparse import ArgumentParser import os import pickle +import re + +# patterns for command matchings +# compiled for extra performance +get_command_pattern: Pattern = re.compile(r"^get\s+[^\s]+$") +put_command_pattern: Pattern = re.compile(r"^put\s+[^\s]+$") +summary_command_pattern: Pattern = re.compile(r"^summary\s+[^\s]+$") +change_command_pattern: Pattern = re.compile(r"^change\s+[^\s]+\s+[^\s]+$") # custome type to represent the hostname(server name) and the server port Address = Tuple[str, int] @@ -29,18 +37,7 @@ class UDPClient: client_socket.connect((self.server_name, self.server_port)) # get command from user - while (command := input(f"myftp> - {self.mode} - : ")) not in [ - "put", - "get", - "summary", - "change", - "help", - "list", - "bye", - ]: - print( - f"myftp> - {self.mode} - : Invalid command. Supported commands are put, get, summary, change, list and help" - ) + command = input(f"myftp> - {self.mode} - : ").strip().lower() # handling the "bye" command if command == "bye": @@ -48,6 +45,7 @@ class UDPClient: print(f"myftp> - {self.mode} - Session is terminated") break + # list files available on the server elif command == "list": client_socket.send(command.encode()) encoded_message, server_address = client_socket.recvfrom(4096) @@ -56,6 +54,44 @@ class UDPClient: client_socket.close() continue + # help + elif command == "help": + continue + + # get command handling + elif get_command_pattern.match(command): + _, filename = command.split(" ", 1) + print( + f"myftp> - {self.mode} - : getting file {filename} from the server" + ) if self.debug else None + + # put command handling + elif put_command_pattern.match(command): + _, filename = command.split(" ", 1) + print( + f"myftp> - {self.mode} - : putting file {filename} into the server" + ) if self.debug else None + + # summary command handling + elif summary_command_pattern.match(command): + _, filename = command.split(" ", 1) + print( + f"myftp> - {self.mode} - : summary file {filename} from the server" + ) if self.debug else None + + # summary command handling + elif change_command_pattern.match(command): + _, old_filename, new_filename = command.split() + print( + f"myftp> - {self.mode} - : changing file named {old_filename} into {new_filename} on the server" + ) if self.debug else None + + else: + print( + f"myftp> - {self.mode} - : Invalid command. Supported commands are put, get, summary, change, list and help. Type help for detailed usage." + ) + continue + client_socket.send(command.encode()) modified_message = client_socket.recv(2048) print(modified_message.decode()) diff --git a/src/myftp/server.py b/src/myftp/server.py index 37b583d..0692db6 100644 --- a/src/myftp/server.py +++ b/src/myftp/server.py @@ -33,9 +33,11 @@ class UDPServer: f"myftp> - {self.mode} - received message from client at {clientAddress}: {message_in_utf8}" ) if self.debug else None + # check for connectivity if message_in_utf8 == "ping": response_message = "pong" + # list files available on server elif message_in_utf8 == "list": encoded_message = pickle.dumps( get_files_in_directory(self.directory_path) @@ -63,7 +65,7 @@ class UDPServer: def get_files_in_directory(directory_path: str) -> list[str]: file_list = [] - for root, _, files in os.walk(directory_path): + for _, _, files in os.walk(directory_path): for file in files: file_list.append(file) return file_list