python ftp обратный вызов функции сервера с клиента - PullRequest
0 голосов
/ 24 февраля 2020

Я создаю простой ftp-сервер / клиент, использующий pyftpdlib и ftplib для получения данных с удаленного компьютера. Я использовал 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...")
...