File overwrite for upload implemented

This commit is contained in:
Kah Kian Fong 2023-03-15 18:10:21 +08:00
parent cc46e29995
commit ea5cf2833f
10 changed files with 79 additions and 22 deletions

View File

@ -50,15 +50,42 @@ def handle_UPLD(conn, args):
bytes_sent = 0
file_size = os.path.getsize(filename)
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:
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("../")
#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("../")
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():
# Get user input

1
clientfile/file1.txt Normal file
View File

@ -0,0 +1 @@
yay i downloadedDownload done

2
clientfile/file2.txt Normal file
View File

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

0
clientfile/file3.txt Normal file
View File

BIN
clientfile/lab9.pdf Normal file

Binary file not shown.

1
clientfile/largefile.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -35,20 +35,44 @@ def handle_upload(conn, args):
filename = args[1]
filesize = int(args[2])
os.chdir(os.path.abspath(SERVER_FILE))
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')
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)')
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')
def handle_download(conn, args):

0
serverfile/file3.txt Normal file
View File

1
serverfile/largefile.txt Normal file

File diff suppressed because one or more lines are too long

1
serverfile/upload.txt Normal file
View File

@ -0,0 +1 @@
Lorem ipsum