LIST and delete works correctly now

This commit is contained in:
Kah Kian Fong 2023-03-15 17:30:27 +08:00
parent a8d942e204
commit cc46e29995
4 changed files with 21 additions and 27 deletions

View File

@ -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())

View File

@ -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')

View File

@ -1 +1 @@
yay i downloaded
yay i downloadedDownload done

View File

@ -1 +1,2 @@
Lorem ipsum
Download done