我正在使用 pyftpdlib 和 ftplib 创建一个简单的 ftp 服务器/客户端,以便从远程计算机接收数据。我使用了一个 ThrottledHandler 来限制带宽,我需要不时更改限制。问题是,当我将服务器部署到远程时,我将无法轻松访问它,以更改带宽限制或停止/启动服务器。所以这是我的问题:
有没有办法在服务器启动并运行时对从客户端更改带宽限制的服务器函数进行回调?
注意:代码现在可以正常工作。
服务器代码:
import os
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.handlers import DTPHandler
from pyftpdlib.handlers import ThrottledDTPHandler
from pyftpdlib.servers import FTPServer
authorizer = DummyAuthorizer()
authorizer.add_user("name", "password", "home_dir", perm="elr",
msg_login="name has logged in.",
msg_quit="name has left the server."
)
throttled_handler = ThrottledDTPHandler
throttled_handler.read_limit = 1024 * 1024 # kbps
throttled_handler.write_limit = 1024 * 1024 # kbps
handler = FTPHandler
handler.use_gmt_times = False
handler.authorizer = authorizer
handler.dtp_handler = throttled_handler
server = FTPServer((IP, PORT), handler)
server.serve_forever()
和客户端代码:
from ftplib import FTP
import os
import datetime
output_folder = "destination_dir"
# Option 1
def listFiles():
print("\n Files in current directory:")
file_list = ftp.nlst()
print(file_list)
# Option 2
def moveInto(folder_name):
if folder_name != "..":
if not folder_name in ftp.nlst():
print("This folder does not exist.")
return
ftp.cwd(folder_name)
return
# Option 3 and 4
def copyAll(copy_path=output_folder):
file_list = []
ftp.retrlines("LIST", callback=file_list.append)
for item in file_list:
# Following lines parse the item details.
file_parts = list(filter(lambda x: x != "", item.split(" ")))
file_name = file_parts[-1]
file_date = file_parts[-4:-1]
if not "." in file_name:
new_path = os.path.join(copy_path, file_name)
ftp.cwd(file_name)
if not os.path.exists(new_path):
os.mkdir(new_path)
print("Folder named {} has been created.".format(file_name))
copyAll(new_path)
else:
new_file_path = os.path.join(copy_path, file_name)
if not os.path.exists(new_file_path):
new_file = open(os.path.join(copy_path, file_name), "wb")
ftp.retrbinary("RETR {}".format(file_name), new_file.write)
new_file.close()
correctModifiedDate(os.path.join(copy_path, file_name), file_date)
print("File named {} has been copied.".format(file_name))
if ftp.pwd() != "/":
ftp.cwd("..")
return
def correctModifiedDate(file_path, correct_date):
# TODO: implement the correction of last modified date.
# print(correct_date)
pass
ftp = FTP("")
ftp.connect(IP, PORT)
ftp.login("name", "password")
print("You have connected to the ftp server.")
while True:
print("\ncommands:")
choice = input("1. List files\n"
"2. Move into\n"
"3. Copy all current folder structure\n"
"4. Continuously copy all current folder structure\n"
"5. Quit "
)
if str(choice) == "1":
listFiles()
elif str(choice) == "2":
folder = input("folder name: ")
moveInto(folder)
elif str(choice) == "3":
copyAll()
elif str(choice) == "4":
while True:
copyAll()
elif str(choice) == "5":
exit_choice = input("\nAre you sure you want to leave the server? Y/N ")
if exit_choice.upper() == "Y":
break
else:
print("You have entered an invalid choice...")