twisted.protocols.ftp.FTPClient 和 Deferreds

发布于 2024-09-11 05:03:46 字数 2190 浏览 0 评论 0原文

和大多数人一样,我花了一段时间才习惯使用 Deferreds,但我正在慢慢地适应。但是,我不清楚如何在使用 Twisted 的 FTP 模块时处理响应,然后使用处理后的响应调用另一个 FTP 命令。我使用 示例 FTP 代码 作为我的起点。

我想连接到 FTP 服务器,获取所有顶级目录,然后输入每个目录并下载所有文件。

首先我连接:

creator = ClientCreator(reactor, FTPClient, config.opts['username'], config.opts['password'], passive=config.opts['passive'])
creator.connectTCP(config.opts['host'], config.opts['port']).addCallback(connectionMade).addErrback(connectionFailed)
reactor.run()

它连接成功,因此我的 connectionMade 函数被调用:

def connectionMade(ftpClient):
    # Get a detailed listing of the current directory
    fileList = FTPFileListProtocol()
    d = ftpClient.list('.', fileList)
    d.addCallbacks(getSortedDirectories, fail, callbackArgs=(fileList,))
    d.addCallback(enterDirs)

如您所见,我排队 getSortedDirectories,然后 enterDirs

def getSortedDirectories(result, fileListProtocol):
    # Go through all directories from greatest to least
    dirs = [directory for directory in sorted(fileListProtocol.files, reverse=True) if directory['filetype'] == 'd']
    return dirs

我的问题是,如何浏览 enterDirs 中的目录?

def enterDirs(dirs):
    for directory in dirs:
        print "We'd be entering '%s' now." % directory

我是否应该将 ftpClient 传递给 enterDirs,就像将 fileList 传递给 getSortedDirectories 一样,然后发出下载请求?

d.addCallback(enterDirs, callbackArgs=(ftpClient,))

def enterDirs(dirs, ftpClient):
    for directory in dirs:
        fileList = FTPFileListProtocol()
        d = ftpClient.list(directory, fileList)
        d.addCallbacks(downloadFiles, fail, callbackArgs=(directory, fileList, ftpClient))

def downloadFiles(result, directory, fileListProtocol, ftpClient):
    for f in fileListProtocol.files if f.filetype == '-':
        fileConsumer = FileConsumer()
        ftpClient.retrieveFile(os.path.join(directory['filename'], file['filename']), fileConsumer)

这是最好的方法吗?

Like most it's taking me a while to get used to using Deferreds but I'm slowly getting there. However, it's not clear to me how I can process a response and then call another FTP command using the processed response when using Twisted's FTP module. I'm using the the example FTP code as my jumping off point.

I want to connect to a FTP server, get all the top level directories, then enter each one and download all the files.

First I connect:

creator = ClientCreator(reactor, FTPClient, config.opts['username'], config.opts['password'], passive=config.opts['passive'])
creator.connectTCP(config.opts['host'], config.opts['port']).addCallback(connectionMade).addErrback(connectionFailed)
reactor.run()

It connects successfully, so my connectionMade function gets called:

def connectionMade(ftpClient):
    # Get a detailed listing of the current directory
    fileList = FTPFileListProtocol()
    d = ftpClient.list('.', fileList)
    d.addCallbacks(getSortedDirectories, fail, callbackArgs=(fileList,))
    d.addCallback(enterDirs)

As you see, I queue up getSortedDirectories and then enterDirs.

def getSortedDirectories(result, fileListProtocol):
    # Go through all directories from greatest to least
    dirs = [directory for directory in sorted(fileListProtocol.files, reverse=True) if directory['filetype'] == 'd']
    return dirs

My question is, how do I go through the directories in enterDirs?

def enterDirs(dirs):
    for directory in dirs:
        print "We'd be entering '%s' now." % directory

Should I be passing ftpClient to enterDirs like fileList is passed to getSortedDirectories and then make my download requests?

d.addCallback(enterDirs, callbackArgs=(ftpClient,))

def enterDirs(dirs, ftpClient):
    for directory in dirs:
        fileList = FTPFileListProtocol()
        d = ftpClient.list(directory, fileList)
        d.addCallbacks(downloadFiles, fail, callbackArgs=(directory, fileList, ftpClient))

def downloadFiles(result, directory, fileListProtocol, ftpClient):
    for f in fileListProtocol.files if f.filetype == '-':
        fileConsumer = FileConsumer()
        ftpClient.retrieveFile(os.path.join(directory['filename'], file['filename']), fileConsumer)

Is this the best approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2024-09-18 05:03:46

我应该将 ftpClient 传递给
EnterDirs 就像 fileList 一样被传递到
getSortedDirectories 然后让我的
下载请求? ......这是
最好的方法?

我确实认为将客户端对象作为参数显式传递确实是最好的方法——大多数情况下,它是备用且优雅的。主要的替代方案是编写一个类并将客户端对象存储到实例变量中,这似乎有点麻烦;在我看来,为此目的使用全局变量是最不理想的选择(全局变量越少越好!-)。

Should I be passing ftpClient to
enterDirs like fileList is passed to
getSortedDirectories and then make my
download requests? ... Is this the
best approach?

I do think that passing the client object explicitly as an argument is indeed the best approach -- mostly, it's spare and elegant. The main alternative would be to code a class and stash the client object into an instance variable, which seems a bit more cumbersome; to use a global variable for the purpose would, in my opinion, be the least desirable alternative (the fewer globals around, the better!-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文