From 57f37c5561ed0daa13d3cadf2ff5bd12a66280de Mon Sep 17 00:00:00 2001 From: Kah Kian Fong Date: Tue, 21 Mar 2023 23:25:49 +0800 Subject: [PATCH] Added for check if file exists before DWLD --- client.py | 27 +++++++++++++++------------ server.py | 50 +++++++++++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/client.py b/client.py index 725621a..8777bf7 100644 --- a/client.py +++ b/client.py @@ -45,18 +45,21 @@ def handle_DWLD(conn, args): filename = args conn.sendall(f"DWLD {filename}\r".encode()) os.chdir(os.path.abspath(CLIENT_FILE)) - filesize = int(conn.recv(BUFFER_SIZE).decode('utf-8')) - bytes_received = 0 - - with open(filename, "wb") as f: - while bytes_received < filesize: - data = conn.recv(BUFFER_SIZE) - f.write(data) - bytes_received += BUFFER_SIZE response = conn.recv(BUFFER_SIZE).decode('utf-8') - print(response) - os.chdir("../") - + if response.startswith("SIZE"): + filesize = response[1] + bytes_received = 0 + with open(filename, "wb") as f: + while bytes_received < filesize: + data = conn.recv(BUFFER_SIZE) + f.write(data) + bytes_received += BUFFER_SIZE + response = conn.recv(BUFFER_SIZE).decode('utf-8') + print(response) + os.chdir("../") + else: + print(response) + os.chdir("../") def handle_UPLD(conn, args): filename = args @@ -106,7 +109,7 @@ def handle_UPLD(conn, args): def handle_HELP(conn, args): conn.sendall(f"HELP\r".encode()) response = conn.recv(BUFFER_SIZE).decode('utf-8') - print(response) + print(response.join()) def user_input(): diff --git a/server.py b/server.py index a3cae65..aa9b378 100644 --- a/server.py +++ b/server.py @@ -54,13 +54,13 @@ def handle_upload(conn, args): bytes_received += BUFFER_SIZE print("Upload successful") os.chdir("../") - conn.sendall(b'Upload done') + conn.sendall(b'200 Upload done') elif response.upper() == 'N': os.chdir("../") conn.sendall(b'Upload cancelled!') else: conn.sendall(b'F') - conn.sendall(b'File does not already exist, Uploading') + conn.sendall(b'150 File does not already exist, Uploading') print(f'Uploading {filename} ({filesize} bytes)') bytes_received = 0 print("\nReceiving...") @@ -73,41 +73,49 @@ def handle_upload(conn, args): bytes_received += BUFFER_SIZE print("Upload successful") os.chdir("../") - conn.sendall(b'Upload done') + conn.sendall(b'200 Upload done') def handle_download(conn, args): filename = args[1] os.chdir(os.path.abspath(SERVER_FILE)) - filesize = os.path.getsize(filename) - conn.sendall(str(filesize).encode('utf-8')) - bytes_sent = 0 - print("\nSending...") - with open(filename, 'rb') as f: - while bytes_sent < filesize: - data = f.read(BUFFER_SIZE) - conn.sendall(data) - bytes_sent += BUFFER_SIZE - print("Download sent") - os.chdir("../") - conn.sendall(b'Download done') + if os.path.exists(filename): + filesize = os.path.getsize(filename) + conn.sendall(str(f"SIZE {filesize}").encode('utf-8')) + bytes_sent = 0 + print("\nSending...") + with open(filename, 'rb') as f: + while bytes_sent < filesize: + data = f.read(BUFFER_SIZE) + conn.sendall(data) + bytes_sent += BUFFER_SIZE + print("Download sent") + os.chdir("../") + conn.sendall(b'200 Download done') + else: + conn.sendall(b'550 File not found.') + os.chdir("../") + def handle_delete(conn, args): filename = args[1] os.chdir(os.path.abspath(SERVER_FILE)) os.remove(filename) - conn.sendall(b'Delete done') + conn.sendall(b'200 Delete done') def handle_rename(conn, args): os.chdir(os.path.abspath(SERVER_FILE)) old_filename = args[1] new_filename = args[2] - os.rename(old_filename, new_filename) - os.chdir("../") - conn.sendall(f"completed".encode('utf-8')) - + if os.path.exists(old_filename): + os.rename(old_filename, new_filename) + os.chdir("../") + conn.sendall(f"completed".encode('utf-8')) + else: + conn.sendall(b'550 File not found.') + os.chdir("../") def handle_quit(conn, args): conn.sendall(b'Quitting') @@ -137,7 +145,7 @@ def handle_cwd(conn, args): conn.sendall(b'200 OK\n') def handle_help(conn, args): - conn.sendall(b'\nList of executable commands:\nLIST: List files\nUPLD : ' + conn.sendall(b'\n214 List of executable commands:\nLIST: List files\nUPLD : ' b'Upload file\nDWLD : Download file\nDELF : Delete file\nRNTO ' b': Rename file\nQUIT: Exit')