Able to upload and download different file types, and of varying sizes.

This commit is contained in:
Kah Kian Fong 2023-03-14 00:13:19 +08:00
parent 78750d95da
commit a8d942e204
2 changed files with 38 additions and 18 deletions

View File

@ -1,7 +1,7 @@
import os import os
import socket import socket
CLIENT_FILE = "clientfile" CLIENT_FILE = "clientfile"
BUFFER_SIZE = 1024
# This prints 1 line at first, then the whole list after running the command again # 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):
@ -20,7 +20,7 @@ def handle_quit(conn, args):
conn.sendall("QUIT\r\n".encode()) conn.sendall("QUIT\r\n".encode())
# Receive response from server # Receive response from server
# Decode bytes to string # Decode bytes to string
response = conn.recv(1024).decode('utf-8').strip() response = conn.recv(BUFFER_SIZE).decode('utf-8').strip()
print(response) print(response)
conn.close() conn.close()
return True return True
@ -31,10 +31,14 @@ 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))
filedata = conn.recv(1024) filesize = int(conn.recv(BUFFER_SIZE).decode('utf-8'))
print(filedata) bytes_received = 0
with open(filename, "wb") as f: with open(filename, "wb") as f:
f.write(filedata) while bytes_received < filesize:
data = conn.recv(BUFFER_SIZE)
f.write(data)
bytes_received += BUFFER_SIZE
os.chdir("../") os.chdir("../")
print("File downloaded successfully") print("File downloaded successfully")
@ -43,7 +47,7 @@ def handle_DWLD(conn, args):
# filename = args # filename = args
# conn.sendall(f"DWLD {filename}\r".encode()) # conn.sendall(f"DWLD {filename}\r".encode())
# filedata = conn.recv(1024) # filedata = conn.recv(BUFFER_SIZE)
# print(filedata) # print(filedata)
# with open(filename, "wb") as f: # with open(filename, "wb") as f:
# f.write(filedata) # f.write(filedata)
@ -55,12 +59,16 @@ def handle_UPLD(conn, args):
if not os.path.exists(filename): if not os.path.exists(filename):
print("File does not exist") print("File does not exist")
else: else:
bytes_sent = 0
file_size = os.path.getsize(filename)
conn.sendall(f"UPLD {filename} {file_size}\r".encode())
with open(filename, "rb") as f: with open(filename, "rb") as f:
filedata = f.read(4096) while bytes_sent < file_size:
file_size = os.path.getsize(filename) filedata = f.read(BUFFER_SIZE)
conn.sendall(f"UPLD {filename} {file_size}\r".encode()) conn.sendall(filedata)
conn.sendall(filedata) bytes_sent += BUFFER_SIZE
response = conn.recv(4096).decode().strip() response = conn.recv(BUFFER_SIZE).decode().strip()
print(response) print(response)
os.chdir("../") os.chdir("../")
@ -81,7 +89,7 @@ def ftp_cient(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port)) sock.connect((host, port))
response = sock.recv(1024).decode().strip() response = sock.recv(BUFFER_SIZE).decode().strip()
print(response) print(response)
while True: while True:

View File

@ -33,9 +33,17 @@ def handle_upload(conn, args):
filesize = int(args[2]) filesize = int(args[2])
os.chdir(os.path.abspath(SERVER_FILE)) os.chdir(os.path.abspath(SERVER_FILE))
print(f'Uploading {filename} ({filesize} bytes)') print(f'Uploading {filename} ({filesize} bytes)')
with open(filename, 'wb') as f:
data = conn.recv(4096) bytes_received = 0
f.write(data) 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
os.chdir("../") os.chdir("../")
conn.sendall(b'Upload done') conn.sendall(b'Upload done')
@ -45,11 +53,15 @@ def handle_download(conn, args):
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.send(struct.pack('i', filesize))
conn.sendall(str(filesize).encode('utf-8'))
bytes_sent = 0
print("\nSending...")
with open(filename, 'rb') as f: with open(filename, 'rb') as f:
data = f.read(4096) while bytes_sent < filesize:
while data: data = f.read(BUFFER_SIZE)
conn.sendall(data) conn.sendall(data)
data = f.read(4096) bytes_sent += BUFFER_SIZE
os.chdir("../") os.chdir("../")
conn.sendall(b'Download done') conn.sendall(b'Download done')