File overwrite for upload implemented
This commit is contained in:
parent
cc46e29995
commit
ea5cf2833f
29
client.py
29
client.py
|
@ -50,7 +50,16 @@ def handle_UPLD(conn, args):
|
||||||
bytes_sent = 0
|
bytes_sent = 0
|
||||||
file_size = os.path.getsize(filename)
|
file_size = os.path.getsize(filename)
|
||||||
conn.sendall(f"UPLD {filename} {file_size}\r".encode())
|
conn.sendall(f"UPLD {filename} {file_size}\r".encode())
|
||||||
|
#does file exist at the server?
|
||||||
|
serverExist = conn.recv(BUFFER_SIZE).decode('utf-8')
|
||||||
|
#if server already has the file
|
||||||
|
if serverExist == 'T':
|
||||||
|
#do you want to overwrite?
|
||||||
|
print(conn.recv(BUFFER_SIZE).decode('utf-8'))
|
||||||
|
overwrite = input()
|
||||||
|
conn.sendall(overwrite.encode('utf-8'))
|
||||||
|
#yes, overwrite
|
||||||
|
if overwrite.upper() == 'Y':
|
||||||
with open(filename, "rb") as f:
|
with open(filename, "rb") as f:
|
||||||
while bytes_sent < file_size:
|
while bytes_sent < file_size:
|
||||||
filedata = f.read(BUFFER_SIZE)
|
filedata = f.read(BUFFER_SIZE)
|
||||||
|
@ -59,6 +68,24 @@ def handle_UPLD(conn, args):
|
||||||
response = conn.recv(BUFFER_SIZE).decode().strip()
|
response = conn.recv(BUFFER_SIZE).decode().strip()
|
||||||
print(response)
|
print(response)
|
||||||
os.chdir("../")
|
os.chdir("../")
|
||||||
|
#no, dont overwrite
|
||||||
|
elif overwrite.upper() == 'N':
|
||||||
|
response = conn.recv(BUFFER_SIZE).decode().strip()
|
||||||
|
print(response)
|
||||||
|
os.chdir("../")
|
||||||
|
#server doesnt have the file
|
||||||
|
elif serverExist == 'F':
|
||||||
|
print(conn.recv(BUFFER_SIZE).decode('utf-8'))
|
||||||
|
with open(filename, "rb") as f:
|
||||||
|
while bytes_sent < file_size:
|
||||||
|
filedata = f.read(BUFFER_SIZE)
|
||||||
|
conn.sendall(filedata)
|
||||||
|
bytes_sent += BUFFER_SIZE
|
||||||
|
response = conn.recv(BUFFER_SIZE).decode().strip()
|
||||||
|
print(response)
|
||||||
|
os.chdir("../")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def user_input():
|
def user_input():
|
||||||
# Get user input
|
# Get user input
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
yay i downloadedDownload done
|
|
@ -0,0 +1,2 @@
|
||||||
|
Lorem ipsum
|
||||||
|
Download done
|
Binary file not shown.
File diff suppressed because one or more lines are too long
26
server.py
26
server.py
|
@ -35,8 +35,32 @@ def handle_upload(conn, args):
|
||||||
filename = args[1]
|
filename = args[1]
|
||||||
filesize = int(args[2])
|
filesize = int(args[2])
|
||||||
os.chdir(os.path.abspath(SERVER_FILE))
|
os.chdir(os.path.abspath(SERVER_FILE))
|
||||||
|
if os.path.exists(filename):
|
||||||
|
conn.sendall(b'T')
|
||||||
|
conn.sendall(b'File already exists! Overwrite? Y/N')
|
||||||
|
# get response Y/N
|
||||||
|
response = conn.recv(BUFFER_SIZE).decode('utf-8')
|
||||||
|
if response.upper() == 'Y':
|
||||||
|
print(f'Uploading {filename} ({filesize} bytes)')
|
||||||
|
bytes_received = 0
|
||||||
|
print("\nReceiving...")
|
||||||
|
with open(os.path.join(os.getcwd(), filename), 'wb') as f:
|
||||||
|
while bytes_received < filesize:
|
||||||
|
data = conn.recv(BUFFER_SIZE)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
f.write(data)
|
||||||
|
bytes_received += BUFFER_SIZE
|
||||||
|
print("Upload successful")
|
||||||
|
os.chdir("../")
|
||||||
|
conn.sendall(b'Upload done')
|
||||||
|
elif response.upper() == 'N':
|
||||||
|
os.chdir("../")
|
||||||
|
conn.sendall(b'Upload cancelled!')
|
||||||
|
else:
|
||||||
|
conn.sendall(b'F')
|
||||||
|
conn.sendall(b'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...")
|
||||||
with open(os.path.join(os.getcwd(), filename), 'wb') as f:
|
with open(os.path.join(os.getcwd(), filename), 'wb') as f:
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
||||||
|
Lorem ipsum
|
Loading…
Reference in New Issue