From cc46e2999588dd75880ae8645b93dccd5996effd Mon Sep 17 00:00:00 2001 From: Kah Kian Fong Date: Wed, 15 Mar 2023 17:30:27 +0800 Subject: [PATCH] LIST and delete works correctly now --- client.py | 36 +++++++++++++----------------------- server.py | 9 ++++++--- serverfile/file1.txt | 2 +- serverfile/file2.txt | 1 + 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/client.py b/client.py index a8f711c..fa23bde 100644 --- a/client.py +++ b/client.py @@ -3,18 +3,17 @@ import socket CLIENT_FILE = "clientfile" BUFFER_SIZE = 1024 -# This prints 1 line at first, then the whole list after running the command again + def handle_list(conn, args): - # Show list of files received from server in response to LIST command - # Response is a multi-line string - response = conn.recv(4096).decode('latin-1').strip() - + num_of_files_received = 0 + num_of_files = int(conn.recv(BUFFER_SIZE).decode('utf-8')) + print("Total files in directory: "+ str(num_of_files)) + while num_of_files_received < num_of_files: + file_info = conn.recv(BUFFER_SIZE).decode('utf-8') + print(file_info) + num_of_files_received += 1 + response = conn.recv(BUFFER_SIZE).decode('utf-8') print(response) - # Check for multi-line response - while response.startswith("1"): - response = conn.recv(4096).decode("latin-1") - print(response) - def handle_quit(conn, args): conn.sendall("QUIT\r\n".encode()) @@ -25,7 +24,6 @@ def handle_quit(conn, args): conn.close() return True - def handle_DWLD(conn, args): # send command over filename = args @@ -39,19 +37,9 @@ def handle_DWLD(conn, args): data = conn.recv(BUFFER_SIZE) f.write(data) bytes_received += BUFFER_SIZE + response = conn.recv(BUFFER_SIZE).decode('utf-8') + print(response) os.chdir("../") - print("File downloaded successfully") - - -# def handle_dwld(conn, args): -# filename = args -# conn.sendall(f"DWLD {filename}\r".encode()) - -# filedata = conn.recv(BUFFER_SIZE) -# print(filedata) -# with open(filename, "wb") as f: -# f.write(filedata) -# print("File downloaded successfully") def handle_UPLD(conn, args): filename = args @@ -113,6 +101,8 @@ def ftp_cient(host, port): elif command.upper() == "DELF": filename = args sock.sendall(f"DELF {filename}\r".encode()) + response = sock.recv(BUFFER_SIZE).decode('utf-8') + print(response) elif command.upper() == "RNTO": oldName, newName = args.split(" ", 1) sock.sendall(f"RNTO {oldName} {newName}\r".encode()) diff --git a/server.py b/server.py index d11b301..0e1927d 100644 --- a/server.py +++ b/server.py @@ -14,6 +14,9 @@ def handle_list(conn, args): #home_directory = os.path.expanduser('~') os.chdir(os.path.abspath(SERVER_FILE)) files = os.listdir(os.getcwd()) + #send number of files over + conn.sendall(str(len(files)).encode('utf-8')) + for file in files: file_info = os.stat(file) file_mode = oct(file_info.st_mode)[-3:] @@ -43,7 +46,7 @@ def handle_upload(conn, args): break f.write(data) bytes_received += BUFFER_SIZE - + print("Upload successful") os.chdir("../") conn.sendall(b'Upload done') @@ -52,7 +55,6 @@ def handle_download(conn, args): filename = args[1] os.chdir(os.path.abspath(SERVER_FILE)) filesize = os.path.getsize(filename) - #conn.send(struct.pack('i', filesize)) conn.sendall(str(filesize).encode('utf-8')) bytes_sent = 0 print("\nSending...") @@ -61,13 +63,14 @@ def handle_download(conn, args): data = f.read(BUFFER_SIZE) conn.sendall(data) bytes_sent += BUFFER_SIZE - + print("Download sent") os.chdir("../") conn.sendall(b'Download done') def handle_delete(conn, args): filename = args[1] + os.chdir(os.path.abspath(SERVER_FILE)) os.remove(filename) conn.sendall(b'Delete done') diff --git a/serverfile/file1.txt b/serverfile/file1.txt index 4ef5359..1944215 100644 --- a/serverfile/file1.txt +++ b/serverfile/file1.txt @@ -1 +1 @@ -yay i downloaded \ No newline at end of file +yay i downloadedDownload done \ No newline at end of file diff --git a/serverfile/file2.txt b/serverfile/file2.txt index 3be11c6..51c1c2e 100644 --- a/serverfile/file2.txt +++ b/serverfile/file2.txt @@ -1 +1,2 @@ Lorem ipsum +Download done \ No newline at end of file