【发布时间】:2015-09-02 04:12:04
【问题描述】:
这是我需要完成的任务: - 连接到 FTP - 获取 test.txt 的内容 - 得到结果后立即将新内容写入test.txt
在实际情况下,我需要获取 previos 修改时间,存储在 txt 文件中,然后仅将那些在该时间之后修改的文件上传到 FTP,而不专门检查每个文件(有数千个,那将是太长了)。
这就是我卡住的地方。
def continueTest(data, ftp):
print(data, ftp)
with open('test.txt', 'w+') as file:
file.write('test')
with open('test.txt', 'rb') as file:
ftp.storbinary('STOR htdocs/test.txt', file)
def test():
host_data=FTP_HOSTS['planz-norwegian']
ftp = ftplib.FTP(host=host_data['server'],
user = host_data['username'],
passwd = host_data['password'])
print('connected to ftp')
ftp.retrbinary('RETR htdocs/test.txt', lambda data:continueTest(data, ftp))
if __name__=='__main__':
test()
这个输出:
connected to ftp
b'test' <ftplib.FTP object at 0x0322FAB0>
Traceback (most recent call last):
File "C:\Python33\Plan Z Editor SL\redistdb.py", line 111, in <module>
test()
File "C:\Python33\Plan Z Editor SL\redistdb.py", line 107, in test
ftp.retrbinary('RETR htdocs/test.txt', lambda data:continueTest(data, ftp))
File "C:\Python33\lib\ftplib.py", line 434, in retrbinary
callback(data)
File "C:\Python33\Plan Z Editor SL\redistdb.py", line 107, in <lambda>
ftp.retrbinary('RETR htdocs/test.txt', lambda data:continueTest(data, ftp))
File "C:\Python33\Plan Z Editor SL\redistdb.py", line 99, in continueTest
ftp.storbinary('STOR htdocs/test.txt', file)
File "C:\Python33\lib\ftplib.py", line 483, in storbinary
with self.transfercmd(cmd, rest) as conn:
File "C:\Python33\lib\ftplib.py", line 391, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python33\lib\ftplib.py", line 351, in ntransfercmd
host, port = self.makepasv()
File "C:\Python33\lib\ftplib.py", line 329, in makepasv
host, port = parse227(self.sendcmd('PASV'))
File "C:\Python33\lib\ftplib.py", line 873, in parse227
raise error_reply(resp)
ftplib.error_reply: 200 Type set to I.
如果我不在回调中使用 STOR,一切正常,但是,我应该如何从 RETR 命令获取数据?
我知道可能的解决方案,但我确信一定有一个更优雅的解决方案:
- 使用 urllib.request 而不是 RETR(如果服务器上没有 HTTP 怎么办?)
- 在回调函数中重新初始化 FTP 连接(由于等待服务器重新连接,可能比预期慢)
- 用户ftp.set_pasv(False)(回调启动,但脚本没有结束,不能使用ftp.quit()或ftp.close())
【问题讨论】: