Python ftplib 超时
我正在尝试使用 ftplib 获取文件列表并下载自上次检查以来的所有新文件。到目前为止,我尝试运行的代码是:
#!/usr/bin/env python
from ftplib import FTP
import sys
host = 'ftp.***.com'
user = '***'
passwd = '***'
try:
ftp = FTP(host)
ftp.login(user, passwd)
except:
print 'Error connecting to FTP server'
sys.exit()
try:
ftp.retrlines('LIST')
except:
print 'Error fetching file listing'
ftp.quit()
sys.exit()
ftp.quit()
每当我运行此代码时,当我尝试检索列表时都会超时。有什么想法吗?
I'm trying to use ftplib to get a file listing and download any new files since my last check. The code I'm trying to run so far is:
#!/usr/bin/env python
from ftplib import FTP
import sys
host = 'ftp.***.com'
user = '***'
passwd = '***'
try:
ftp = FTP(host)
ftp.login(user, passwd)
except:
print 'Error connecting to FTP server'
sys.exit()
try:
ftp.retrlines('LIST')
except:
print 'Error fetching file listing'
ftp.quit()
sys.exit()
ftp.quit()
Whenever I run this it times out when I try to retrieve the listing. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果被动模式由于某种原因失败,请尝试:
使用主动模式。
If Passive Mode is failing for some reason try:
to use Active Mode.
最有可能的是主动模式和被动模式之间的冲突。确保满足以下条件之一:
编辑:我查看了文档,发现在 Python 2.1 及更高版本中默认是被动模式。您正在与什么服务器通信,您知道它是否支持被动模式吗?
在主动模式(非 PASV)下,客户端发送 PORT 命令,告诉服务器在该端口上启动 DATA 连接,这要求您的防火墙了解 PORT 命令,以便它可以将传入的 DATA 连接转发给您 - 很少有防火墙支持这一点。在被动模式下,客户端打开数据连接,服务器使用它(服务器在打开数据连接时是“被动”的)。
万一您不使用被动模式,请执行
ftp.set_pasv(True)
并查看是否有影响。Most likely a conflict between Active and Passive mode. Make sure that one of the following is true:
EDIT: I looked at the docs, and found that in Python 2.1 and later the default is passive mode. What server are you talking to, and di you know if it supports passive mode?
In active mode (non-PASV) the client sends a PORT command telling the server to initiate the DATA connection on that port, which requires your firewall be aware of the PORT command so it can forward the incoming DATA connection to you -- few firewalls support this. In passive mode the client opens the DATA connection and the server uses it (the server is "passive" in opening the data connection).
Just in case you're not using passive mode, do a
ftp.set_pasv(True)
and see if that makes a difference.