feat(UDP): commands pattern matching
- Added comments
This commit is contained in:
parent
4c1dc9428c
commit
1eb4e072ca
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
Zero. Only python standard libs were used.
|
Zero. Only python standard libs were used. Tested on Python 3.11
|
||||||
|
|
||||||
## Running
|
## Running
|
||||||
|
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
from socket import socket, AF_INET, SOCK_DGRAM
|
from socket import socket, AF_INET, SOCK_DGRAM
|
||||||
from typing import Tuple
|
from typing import Pattern, Tuple
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
import os
|
import os
|
||||||
import pickle
|
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
|
# custome type to represent the hostname(server name) and the server port
|
||||||
Address = Tuple[str, int]
|
Address = Tuple[str, int]
|
||||||
@ -29,18 +37,7 @@ class UDPClient:
|
|||||||
client_socket.connect((self.server_name, self.server_port))
|
client_socket.connect((self.server_name, self.server_port))
|
||||||
|
|
||||||
# get command from user
|
# get command from user
|
||||||
while (command := input(f"myftp> - {self.mode} - : ")) not in [
|
command = input(f"myftp> - {self.mode} - : ").strip().lower()
|
||||||
"put",
|
|
||||||
"get",
|
|
||||||
"summary",
|
|
||||||
"change",
|
|
||||||
"help",
|
|
||||||
"list",
|
|
||||||
"bye",
|
|
||||||
]:
|
|
||||||
print(
|
|
||||||
f"myftp> - {self.mode} - : Invalid command. Supported commands are put, get, summary, change, list and help"
|
|
||||||
)
|
|
||||||
|
|
||||||
# handling the "bye" command
|
# handling the "bye" command
|
||||||
if command == "bye":
|
if command == "bye":
|
||||||
@ -48,6 +45,7 @@ class UDPClient:
|
|||||||
print(f"myftp> - {self.mode} - Session is terminated")
|
print(f"myftp> - {self.mode} - Session is terminated")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# list files available on the server
|
||||||
elif command == "list":
|
elif command == "list":
|
||||||
client_socket.send(command.encode())
|
client_socket.send(command.encode())
|
||||||
encoded_message, server_address = client_socket.recvfrom(4096)
|
encoded_message, server_address = client_socket.recvfrom(4096)
|
||||||
@ -56,6 +54,44 @@ class UDPClient:
|
|||||||
client_socket.close()
|
client_socket.close()
|
||||||
continue
|
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())
|
client_socket.send(command.encode())
|
||||||
modified_message = client_socket.recv(2048)
|
modified_message = client_socket.recv(2048)
|
||||||
print(modified_message.decode())
|
print(modified_message.decode())
|
||||||
|
@ -33,9 +33,11 @@ class UDPServer:
|
|||||||
f"myftp> - {self.mode} - received message from client at {clientAddress}: {message_in_utf8}"
|
f"myftp> - {self.mode} - received message from client at {clientAddress}: {message_in_utf8}"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
|
# check for connectivity
|
||||||
if message_in_utf8 == "ping":
|
if message_in_utf8 == "ping":
|
||||||
response_message = "pong"
|
response_message = "pong"
|
||||||
|
|
||||||
|
# list files available on server
|
||||||
elif message_in_utf8 == "list":
|
elif message_in_utf8 == "list":
|
||||||
encoded_message = pickle.dumps(
|
encoded_message = pickle.dumps(
|
||||||
get_files_in_directory(self.directory_path)
|
get_files_in_directory(self.directory_path)
|
||||||
@ -63,7 +65,7 @@ class UDPServer:
|
|||||||
|
|
||||||
def get_files_in_directory(directory_path: str) -> list[str]:
|
def get_files_in_directory(directory_path: str) -> list[str]:
|
||||||
file_list = []
|
file_list = []
|
||||||
for root, _, files in os.walk(directory_path):
|
for _, _, files in os.walk(directory_path):
|
||||||
for file in files:
|
for file in files:
|
||||||
file_list.append(file)
|
file_list.append(file)
|
||||||
return file_list
|
return file_list
|
||||||
|
Loading…
x
Reference in New Issue
Block a user