LIST and delete works correctly now
This commit is contained in:
parent
a8d942e204
commit
cc46e29995
36
client.py
36
client.py
|
@ -3,18 +3,17 @@ import socket
|
||||||
CLIENT_FILE = "clientfile"
|
CLIENT_FILE = "clientfile"
|
||||||
BUFFER_SIZE = 1024
|
BUFFER_SIZE = 1024
|
||||||
|
|
||||||
# This prints 1 line at first, then the whole list after running the command again
|
|
||||||
def handle_list(conn, args):
|
def handle_list(conn, args):
|
||||||
# Show list of files received from server in response to LIST command
|
num_of_files_received = 0
|
||||||
# Response is a multi-line string
|
num_of_files = int(conn.recv(BUFFER_SIZE).decode('utf-8'))
|
||||||
response = conn.recv(4096).decode('latin-1').strip()
|
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)
|
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):
|
def handle_quit(conn, args):
|
||||||
conn.sendall("QUIT\r\n".encode())
|
conn.sendall("QUIT\r\n".encode())
|
||||||
|
@ -25,7 +24,6 @@ def handle_quit(conn, args):
|
||||||
conn.close()
|
conn.close()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def handle_DWLD(conn, args):
|
def handle_DWLD(conn, args):
|
||||||
# send command over
|
# send command over
|
||||||
filename = args
|
filename = args
|
||||||
|
@ -39,19 +37,9 @@ def handle_DWLD(conn, args):
|
||||||
data = conn.recv(BUFFER_SIZE)
|
data = conn.recv(BUFFER_SIZE)
|
||||||
f.write(data)
|
f.write(data)
|
||||||
bytes_received += BUFFER_SIZE
|
bytes_received += BUFFER_SIZE
|
||||||
|
response = conn.recv(BUFFER_SIZE).decode('utf-8')
|
||||||
|
print(response)
|
||||||
os.chdir("../")
|
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):
|
def handle_UPLD(conn, args):
|
||||||
filename = args
|
filename = args
|
||||||
|
@ -113,6 +101,8 @@ def ftp_cient(host, port):
|
||||||
elif command.upper() == "DELF":
|
elif command.upper() == "DELF":
|
||||||
filename = args
|
filename = args
|
||||||
sock.sendall(f"DELF {filename}\r".encode())
|
sock.sendall(f"DELF {filename}\r".encode())
|
||||||
|
response = sock.recv(BUFFER_SIZE).decode('utf-8')
|
||||||
|
print(response)
|
||||||
elif command.upper() == "RNTO":
|
elif command.upper() == "RNTO":
|
||||||
oldName, newName = args.split(" ", 1)
|
oldName, newName = args.split(" ", 1)
|
||||||
sock.sendall(f"RNTO {oldName} {newName}\r".encode())
|
sock.sendall(f"RNTO {oldName} {newName}\r".encode())
|
||||||
|
|
|
@ -14,6 +14,9 @@ def handle_list(conn, args):
|
||||||
#home_directory = os.path.expanduser('~')
|
#home_directory = os.path.expanduser('~')
|
||||||
os.chdir(os.path.abspath(SERVER_FILE))
|
os.chdir(os.path.abspath(SERVER_FILE))
|
||||||
files = os.listdir(os.getcwd())
|
files = os.listdir(os.getcwd())
|
||||||
|
#send number of files over
|
||||||
|
conn.sendall(str(len(files)).encode('utf-8'))
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
file_info = os.stat(file)
|
file_info = os.stat(file)
|
||||||
file_mode = oct(file_info.st_mode)[-3:]
|
file_mode = oct(file_info.st_mode)[-3:]
|
||||||
|
@ -43,7 +46,7 @@ def handle_upload(conn, args):
|
||||||
break
|
break
|
||||||
f.write(data)
|
f.write(data)
|
||||||
bytes_received += BUFFER_SIZE
|
bytes_received += BUFFER_SIZE
|
||||||
|
print("Upload successful")
|
||||||
os.chdir("../")
|
os.chdir("../")
|
||||||
conn.sendall(b'Upload done')
|
conn.sendall(b'Upload done')
|
||||||
|
|
||||||
|
@ -52,7 +55,6 @@ def handle_download(conn, args):
|
||||||
filename = args[1]
|
filename = args[1]
|
||||||
os.chdir(os.path.abspath(SERVER_FILE))
|
os.chdir(os.path.abspath(SERVER_FILE))
|
||||||
filesize = os.path.getsize(filename)
|
filesize = os.path.getsize(filename)
|
||||||
#conn.send(struct.pack('i', filesize))
|
|
||||||
conn.sendall(str(filesize).encode('utf-8'))
|
conn.sendall(str(filesize).encode('utf-8'))
|
||||||
bytes_sent = 0
|
bytes_sent = 0
|
||||||
print("\nSending...")
|
print("\nSending...")
|
||||||
|
@ -61,13 +63,14 @@ def handle_download(conn, args):
|
||||||
data = f.read(BUFFER_SIZE)
|
data = f.read(BUFFER_SIZE)
|
||||||
conn.sendall(data)
|
conn.sendall(data)
|
||||||
bytes_sent += BUFFER_SIZE
|
bytes_sent += BUFFER_SIZE
|
||||||
|
print("Download sent")
|
||||||
os.chdir("../")
|
os.chdir("../")
|
||||||
conn.sendall(b'Download done')
|
conn.sendall(b'Download done')
|
||||||
|
|
||||||
|
|
||||||
def handle_delete(conn, args):
|
def handle_delete(conn, args):
|
||||||
filename = args[1]
|
filename = args[1]
|
||||||
|
os.chdir(os.path.abspath(SERVER_FILE))
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
conn.sendall(b'Delete done')
|
conn.sendall(b'Delete done')
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
yay i downloaded
|
yay i downloadedDownload done
|
|
@ -1 +1,2 @@
|
||||||
Lorem ipsum
|
Lorem ipsum
|
||||||
|
Download done
|
Loading…
Reference in New Issue