Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python ftp从客户端回调服务器函数_Python_Ftplib_Pyftpdlib - Fatal编程技术网

python ftp从客户端回调服务器函数

python ftp从客户端回调服务器函数,python,ftplib,pyftpdlib,Python,Ftplib,Pyftpdlib,我正在使用pyftpdlib和ftplib创建一个简单的ftp服务器/客户端,以便从远程计算机接收数据。为了限制带宽,我使用了ThrottledHandler,我需要不时地更改限制。问题是,当我将服务器部署到远程时,我将无法轻松访问它,无法更改带宽限制或停止/启动服务器。所以我的问题是: 有没有一种方法可以在服务器启动并运行时,从客户端对更改带宽限制的服务器函数进行回调 注意:代码现在运行没有任何问题 服务器代码: import os from pyftpdlib.authorizers im

我正在使用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...")

您可以将服务器重写为一种方法,该方法包括**kwargs,以便在启动脚本时接收带宽变量。如果您还使用单独的启动/关闭方法,则可以通过ssh远程运行这些命令。我建议“建议”使用rpc服务器或更简单的基本套接字服务器启动一个线程,该服务器允许您通过rpc或作为套接字服务器中发送和解析的字符串调用具有2个值的回调。