Added for check if file exists before DWLD
This commit is contained in:
parent
f843bc3440
commit
57f37c5561
11
client.py
11
client.py
|
@ -45,9 +45,10 @@ 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'))
|
response = conn.recv(BUFFER_SIZE).decode('utf-8')
|
||||||
|
if response.startswith("SIZE"):
|
||||||
|
filesize = response[1]
|
||||||
bytes_received = 0
|
bytes_received = 0
|
||||||
|
|
||||||
with open(filename, "wb") as f:
|
with open(filename, "wb") as f:
|
||||||
while bytes_received < filesize:
|
while bytes_received < filesize:
|
||||||
data = conn.recv(BUFFER_SIZE)
|
data = conn.recv(BUFFER_SIZE)
|
||||||
|
@ -56,7 +57,9 @@ def handle_DWLD(conn, args):
|
||||||
response = conn.recv(BUFFER_SIZE).decode('utf-8')
|
response = conn.recv(BUFFER_SIZE).decode('utf-8')
|
||||||
print(response)
|
print(response)
|
||||||
os.chdir("../")
|
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():
|
||||||
|
|
24
server.py
24
server.py
|
@ -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,14 +73,15 @@ 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))
|
||||||
|
if os.path.exists(filename):
|
||||||
filesize = os.path.getsize(filename)
|
filesize = os.path.getsize(filename)
|
||||||
conn.sendall(str(filesize).encode('utf-8'))
|
conn.sendall(str(f"SIZE {filesize}").encode('utf-8'))
|
||||||
bytes_sent = 0
|
bytes_sent = 0
|
||||||
print("\nSending...")
|
print("\nSending...")
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
|
@ -90,24 +91,31 @@ def handle_download(conn, args):
|
||||||
bytes_sent += BUFFER_SIZE
|
bytes_sent += BUFFER_SIZE
|
||||||
print("Download sent")
|
print("Download sent")
|
||||||
os.chdir("../")
|
os.chdir("../")
|
||||||
conn.sendall(b'Download done')
|
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]
|
||||||
|
if os.path.exists(old_filename):
|
||||||
os.rename(old_filename, new_filename)
|
os.rename(old_filename, new_filename)
|
||||||
os.chdir("../")
|
os.chdir("../")
|
||||||
conn.sendall(f"completed".encode('utf-8'))
|
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')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue