twisted.protocols.ftp.FTPClient 和 Deferreds
和大多数人一样,我花了一段时间才习惯使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确实认为将客户端对象作为参数显式传递确实是最好的方法——大多数情况下,它是备用且优雅的。主要的替代方案是编写一个类并将客户端对象存储到实例变量中,这似乎有点麻烦;在我看来,为此目的使用全局变量是最不理想的选择(全局变量越少越好!-)。
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!-).