Added serverfile for ftp server file storage, added clientfile for ftp client file storage, able to list, upload, download and rename

This commit is contained in:
Kah Kian Fong 2023-03-13 20:34:15 +08:00
parent 4b8425cea8
commit 1d8affbb07
9 changed files with 106 additions and 23 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

8
.idea/Python_FTP.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.10" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,15 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true">
<option name="myValues">
<value>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="time" />
</list>
</value>
</option>
<option name="myCustomValuesEnabled" value="true" />
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (Python_FTP)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Python_FTP.iml" filepath="$PROJECT_DIR$/.idea/Python_FTP.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -1,16 +1,18 @@
import os
import socket
CLIENT_FILE = "clientfile"
# This prints 1 line at first, then the whole list after running the command again
def handle_list(conn, args):
# Show list of files received from server in response to LIST command
# Response is a multi-line string
response = conn.recv(1024).decode('latin-1').strip()
print(response)
response = conn.recv(4096).decode('latin-1').strip()
print(response)
# Check for multi-line response
while response.startswith("1"):
response = conn.recv(1024).decode("latin-1")
response = conn.recv(4096).decode("latin-1")
print(response)
@ -24,18 +26,32 @@ def handle_quit(conn, args):
return True
def handle_dwld(conn, args):
def handle_DWLD(conn, args):
# send command over
filename = args
conn.sendall(f"DWLD {filename}\r".encode())
os.chdir(os.path.abspath(CLIENT_FILE))
filedata = conn.recv(1024)
print(filedata)
with open(filename, "wb") as f:
f.write(filedata)
os.chdir("../")
print("File downloaded successfully")
# def handle_dwld(conn, args):
# filename = args
# conn.sendall(f"DWLD {filename}\r".encode())
# filedata = conn.recv(1024)
# print(filedata)
# with open(filename, "wb") as f:
# f.write(filedata)
# print("File downloaded successfully")
def handle_UPLD(conn, args):
filename = args
os.chdir(os.path.abspath(CLIENT_FILE))
if not os.path.exists(filename):
print("File does not exist")
else:
@ -44,8 +60,9 @@ def handle_UPLD(conn, args):
file_size = os.path.getsize(filename)
conn.sendall(f"UPLD {filename} {file_size}\r".encode())
conn.sendall(filedata)
response = conn.recv(1024).decode().strip()
response = conn.recv(4096).decode().strip()
print(response)
os.chdir("../")
def user_input():
# Get user input
@ -82,7 +99,7 @@ def ftp_cient(host, port):
handle_quit(sock, args)
break
elif command.upper() == "DWLD":
handle_dwld(sock, args)
handle_DWLD(sock, args)
elif command.upper() == "UPLD":
handle_UPLD(sock, args)
elif command.upper() == "DELF":

View File

@ -7,9 +7,12 @@ import struct
import time
BUFFER_SIZE = 1024
SERVER_FILE = "serverfile"
def handle_list(conn, args):
#conn.send(b'150 Opening ASCII mode data connection for file list.\n')
#home_directory = os.path.expanduser('~')
os.chdir(os.path.abspath(SERVER_FILE))
files = os.listdir(os.getcwd())
for file in files:
file_info = os.stat(file)
@ -18,64 +21,71 @@ def handle_list(conn, args):
file_time = file_info.st_mtime
file_date = time.strftime('%b %d %H:%M', time.gmtime(file_time))
file_name = file.encode('utf-8') + b'\r\n'
conn.send(file_mode.encode('utf-8') + b' 1 user user ' + str(file_size).encode('utf-8') + b' ' + file_date.encode('utf-8') + b' ' + file_name)
conn.sendall(file_mode.encode('utf-8') + b' 1 user user ' + str(file_size).encode('utf-8') + b' ' + file_date.encode('utf-8') + b' ' + file_name)
if not files:
conn.send(b'226 No files in directory.\n')
conn.send(b'226 Transfer complete.\n')
conn.sendall(b'226 No files in directory.\n')
os.chdir("../")
conn.sendall(b'226 Transfer complete.\n')
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)')
with open(filename, 'wb') as f:
data = conn.recv(4096)
f.write(data)
conn.send(b'done')
os.chdir("../")
conn.sendall(b'Upload done')
def handle_download(conn, args):
filename = args[1]
os.chdir(os.path.abspath(SERVER_FILE))
filesize = os.path.getsize(filename)
#conn.send(struct.pack('i', filesize))
with open(filename, 'rb') as f:
data = f.read(4096)
while data:
conn.send(data)
conn.sendall(data)
data = f.read(4096)
conn.send(b'done')
os.chdir("../")
conn.sendall(b'Download done')
def handle_delete(conn, args):
filename = args[1]
os.remove(filename)
conn.send(b'done')
conn.sendall(b'Delete done')
def handle_rename(conn, args):
os.chdir(os.path.abspath(SERVER_FILE))
old_filename = args[1]
new_filename = args[2]
os.rename(old_filename, new_filename)
conn.send(b'done')
os.chdir("../")
conn.sendall(f"completed".encode('utf-8'))
def handle_quit(conn, args):
conn.send(b'done')
conn.sendall(b'Quitting')
conn.close()
return True
def handle_user(conn, args):
conn.send(b'230 Login successful.\n')
conn.sendall(b'230 Login successful.\n')
def handle_pwd(conn, args):
current_dir = os.getcwd()
conn.send(f'257 "{current_dir}" is the current directory.\n'.encode())
conn.sendall(f'257 "{current_dir}" is the current directory.\n'.encode())
handle_list(conn, args)
def handle_port(conn, args):
conn.send(b'200 OK\n')
conn.sendall(b'200 OK\n')
commands = {
'LIST': handle_list,
@ -86,15 +96,19 @@ commands = {
'QUIT': handle_quit,
'USER': handle_user,
'PWD': handle_pwd,
'TYPE': lambda conn, args: conn.send(b'200 OK\n'),
'TYPE': lambda conn, args: conn.sendall(b'200 OK\n'),
'PORT': handle_port,
'CDUP': lambda conn, args: conn.send(b'200 OK\n'),
'CDUP': lambda conn, args: conn.sendall(b'200 OK\n'),
}
def handle_connection(conn):
conn.send(b'220 Welcome to the FTP server.\n')
conn.send(b'220 Welcome to the FTP server.\nList 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'<new_name>: Rename file\nQUIT: Exit')
while True:
data = conn.recv(BUFFER_SIZE).decode()
print(f'Server received: {data}')
if not data:
@ -104,10 +118,12 @@ def handle_connection(conn):
print(f'Command: {command}')
if command in commands:
if commands == handle_quit:
conn.sendall(b'Closing Connection')
conn.close()
break
commands[command](conn, args)
else:
conn.send(b'Invalid command.\n')
conn.sendall(b'Invalid command.\n')
# For FTP client testing
#handle_pwd(conn, [])