feat(udp): use true byte representation

- added an option to make client without debug
This commit is contained in:
minhtrannhat 2023-12-01 16:51:11 -05:00
parent d78eb940cd
commit 5519be9222
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
3 changed files with 17 additions and 3 deletions

View File

@ -9,6 +9,9 @@ server:
client:
docker exec -it project-ftp_client-1 python client.py --directory /client_directory --debug 1
client-no-debug:
docker exec -it project-ftp_client-1 python client.py --directory /client_directory
clean:
docker-compose down --volumes

View File

@ -74,7 +74,7 @@ class UDPClient:
# help
elif command == "help":
request_payload: str = help_requrest_opcode + "00000"
request_payload: str = help_requrest_opcode + "00000" # 10000000
print(
f"myftp> - {self.mode} - Asking for help from the server"
) if self.debug else None
@ -113,7 +113,10 @@ class UDPClient:
)
continue
client_socket.sendto(request_payload.encode("utf-8"), (self.server_name, self.server_port))
# convert the payload to bytes so it can be sent to the server
byte_representation_req_payload: bytes = bytes([int(request_payload, 2)])
client_socket.sendto(byte_representation_req_payload, (self.server_name, self.server_port))
response_payload = client_socket.recv(2048)

View File

@ -48,7 +48,15 @@ class UDPServer:
try:
while not shut_down:
message, clientAddress = self.server_socket.recvfrom(2048)
request_payload = message.decode()
# decode for quick and dirty commands like ping and list server files
# outside of the scope of the project
try:
request_payload = message.decode()
except UnicodeDecodeError:
# most commands (get, put, summary ...) will be handled by this catch block
request_payload: str = bin(int.from_bytes(message, byteorder='big'))[2:]
print(
f"myftp> - {self.mode} ------------------------------------------------------------------"