Added for check if file exists before DWLD

This commit is contained in:
Kah Kian Fong 2023-03-21 23:25:49 +08:00
parent f843bc3440
commit 57f37c5561
2 changed files with 44 additions and 33 deletions

View File

@ -45,18 +45,21 @@ def handle_DWLD(conn, args):
filename = args filename = args
conn.sendall(f"DWLD {filename}\r".encode()) conn.sendall(f"DWLD {filename}\r".encode())
os.chdir(os.path.abspath(CLIENT_FILE)) 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') response = conn.recv(BUFFER_SIZE).decode('utf-8')
print(response) if response.startswith("SIZE"):
os.chdir("../") 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): def handle_UPLD(conn, args):
filename = args filename = args
@ -106,7 +109,7 @@ def handle_UPLD(conn, args):
def handle_HELP(conn, args): def handle_HELP(conn, args):
conn.sendall(f"HELP\r".encode()) conn.sendall(f"HELP\r".encode())
response = conn.recv(BUFFER_SIZE).decode('utf-8') response = conn.recv(BUFFER_SIZE).decode('utf-8')
print(response) print(response.join())
def user_input(): def user_input():

View File

@ -54,13 +54,13 @@ def handle_upload(conn, args):
bytes_received += BUFFER_SIZE bytes_received += BUFFER_SIZE
print("Upload successful") print("Upload successful")
os.chdir("../") os.chdir("../")
conn.sendall(b'Upload done') conn.sendall(b'200 Upload done')
elif response.upper() == 'N': elif response.upper() == 'N':
os.chdir("../") os.chdir("../")
conn.sendall(b'Upload cancelled!') conn.sendall(b'Upload cancelled!')
else: else:
conn.sendall(b'F') 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)') print(f'Uploading {filename} ({filesize} bytes)')
bytes_received = 0 bytes_received = 0
print("\nReceiving...") print("\nReceiving...")
@ -73,41 +73,49 @@ def handle_upload(conn, args):
bytes_received += BUFFER_SIZE bytes_received += BUFFER_SIZE
print("Upload successful") print("Upload successful")
os.chdir("../") os.chdir("../")
conn.sendall(b'Upload done') conn.sendall(b'200 Upload done')
def handle_download(conn, args): 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) if os.path.exists(filename):
conn.sendall(str(filesize).encode('utf-8')) filesize = os.path.getsize(filename)
bytes_sent = 0 conn.sendall(str(f"SIZE {filesize}").encode('utf-8'))
print("\nSending...") bytes_sent = 0
with open(filename, 'rb') as f: print("\nSending...")
while bytes_sent < filesize: with open(filename, 'rb') as f:
data = f.read(BUFFER_SIZE) while bytes_sent < filesize:
conn.sendall(data) data = f.read(BUFFER_SIZE)
bytes_sent += BUFFER_SIZE conn.sendall(data)
print("Download sent") bytes_sent += BUFFER_SIZE
os.chdir("../") print("Download sent")
conn.sendall(b'Download done') os.chdir("../")
conn.sendall(b'200 Download done')
else:
conn.sendall(b'550 File not found.')
os.chdir("../")
def handle_delete(conn, args): def handle_delete(conn, args):
filename = args[1] filename = args[1]
os.chdir(os.path.abspath(SERVER_FILE)) os.chdir(os.path.abspath(SERVER_FILE))
os.remove(filename) os.remove(filename)
conn.sendall(b'Delete done') conn.sendall(b'200 Delete done')
def handle_rename(conn, args): def handle_rename(conn, args):
os.chdir(os.path.abspath(SERVER_FILE)) os.chdir(os.path.abspath(SERVER_FILE))
old_filename = args[1] old_filename = args[1]
new_filename = args[2] new_filename = args[2]
os.rename(old_filename, new_filename) if os.path.exists(old_filename):
os.chdir("../") os.rename(old_filename, new_filename)
conn.sendall(f"completed".encode('utf-8')) os.chdir("../")
conn.sendall(f"completed".encode('utf-8'))
else:
conn.sendall(b'550 File not found.')
os.chdir("../")
def handle_quit(conn, args): def handle_quit(conn, args):
conn.sendall(b'Quitting') conn.sendall(b'Quitting')
@ -137,7 +145,7 @@ def handle_cwd(conn, args):
conn.sendall(b'200 OK\n') conn.sendall(b'200 OK\n')
def handle_help(conn, args): def handle_help(conn, args):
conn.sendall(b'\nList of executable commands:\nLIST: List files\nUPLD <file_name>: ' conn.sendall(b'\n214 List of executable commands:\nLIST: List files\nUPLD <file_name>: '
b'Upload file\nDWLD <file_name>: Download file\nDELF <file_name>: Delete file\nRNTO <old_name> ' b'Upload file\nDWLD <file_name>: Download file\nDELF <file_name>: Delete file\nRNTO <old_name> '
b'<new_name>: Rename file\nQUIT: Exit') b'<new_name>: Rename file\nQUIT: Exit')