FTP上载文件手动工作,但使用Python ftplib失败

FTP上载文件手动工作,但使用Python ftplib失败,python,ftp,Python,Ftp,我在Debian框中安装了vsFTP。当使用ftp命令手动上传文件时,这是正常的。i、 e.以下会议有效: john@myhost:~$ ftp xxx.xxx.xxx.xxx 5111 Connected to xxx.xxx.xxx.xxx. 220 Hello,Welcom to my FTP server. Name (xxx.xxx.xxx.xxx:john): ftpuser 331 Please specify the password. Password: 230 Login s

我在Debian框中安装了vsFTP。当使用ftp命令手动上传文件时,这是正常的。i、 e.以下会议有效:

john@myhost:~$ ftp xxx.xxx.xxx.xxx 5111 Connected to xxx.xxx.xxx.xxx. 220 Hello,Welcom to my FTP server. Name (xxx.xxx.xxx.xxx:john): ftpuser 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> put st.zip local: st.zip remote: st.zip 200 PORT command successful. Consider using PASV. 150 Ok to send data. 226 File receive OK. 12773 bytes sent in 0.00 secs (277191.8 kB/s) ftp> 221 Goodbye. john@myhost:~$ftp xxx.xxx.xxx.xxx 5111 已连接到xxx.xxx.xxx.xxx。 220你好,欢迎来到我的FTP服务器。 姓名(xxx.xxx.xxx.xxx:john):ftpuser 请指定密码。 密码: 230登录成功。 远程系统类型为UNIX。 使用二进制模式传输文件。 ftp>put st.zip 本地:st.zip远程:st.zip 200端口命令成功。考虑使用PASV。 150确定发送数据。 226文件接收正常。 0.00秒发送12773字节(277191.8 kB/s) 再见。 (请注意,如上所述,出于某种原因,我将vsFTP服务器配置为使用非默认端口,例如5111)

现在,当我用python编写脚本以编程方式上载文件时,它失败了。错误显示为“超时”,如以下会话所示:

john@myhost:~$ ipython Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) Type "copyright", "credits" or "license" for more information. IPython 0.8.4 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: import ftplib In [2]: ftp=ftplib.FTP() In [3]: ftp.connect('xxx.xxx.xxx.xxx','5111') Out[3]: "220 Hello,Welcom to my FTP server." In [4]: ftp.login('ftpuser','ftpuser') Out[4]: '230 Login successful.' In [5]: f=open('st.zip','rb') In [6]: ftp.storbinary('STOR %s' % 'my_ftp_file.zip', f) --------------------------------------------------------------------------- error Traceback (most recent call last) ... /usr/lib/python2.5/ftplib.pyc in ntransfercmd(self, cmd, rest) 322 af, socktype, proto, canon, sa = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0] 323 conn = socket.socket(af, socktype, proto) --> 324 conn.connect(sa) 325 if rest is not None: 326 self.sendcmd("REST %s" % rest) /usr/lib/python2.5/socket.pyc in connect(self, *args) error: (110, 'Connection timed out') john@myhost:~$ipython Python 2.5.2(r252:60911,2010年1月24日,14:53:14) 有关详细信息,请键入“版权”、“信用”或“许可证”。 IPython 0.8.4——一种增强的交互式Python。 ? -> 介绍和概述IPython的功能。 %快速参考->快速参考。 帮助->Python自己的帮助系统。 对象?->有关“对象”的详细信息?对象也可以工作??打印更多。 在[1]中:导入ftplib 在[2]中:ftp=ftplib.ftp() [3]中的ftp.connect('xxx.xxx.xxx.xxx','5111') Out[3]:“您好,欢迎访问我的FTP服务器。” [4]中的ftp.login('ftpuser','ftpuser') Out[4]:“230登录成功。” 在[5]中:f=open('st.zip','rb') [6]中的ftp.storbinary('STOR%s''my\u ftp\u file.zip',f) --------------------------------------------------------------------------- 错误回溯(最近一次呼叫上次) ... /ntransfercmd中的usr/lib/python2.5/ftplib.pyc(self、cmd、rest) 322 af,socktype,proto,canon,sa=socket.getaddrinfo(主机,端口,0,socket.SOCK_流)[0] 323连接=插座。插座(af,插座类型,原型) -->324连接(sa) 325如果rest不是None: 326 self.sendcmd(“REST%s”%REST) /连接中的usr/lib/python2.5/socket.pyc(self,*args) 错误:(110,‘连接超时’) 我猜在我的vsFTP服务器中有一些错误的配置,但无法找到它。有人能帮忙吗

我的vsFTP配置是:

listen=YES connect_from_port_20=YES listen_port=5111 ftp_data_port=5110 # Passive FTP mode allowed pasv_enable=YES pasv_min_port=5300 pasv_max_port=5400 max_per_ip=2 听=是的 从\u端口\u 20连接\u=是 监听端口=5111 ftp_数据_端口=5110 #允许被动FTP模式 pasv_启用=是 pasv_最小_端口=5300 pasv_最大_端口=5400 每个ip的最大值=2
在您尝试发送数据之前,超时不会发生,因此您能够成功连接到服务器。我看到的唯一区别是ftplib默认使用被动模式,而您的命令行客户端似乎不使用被动模式。试着做

ftp.set_pasv(False)
在开始转移之前,看看会发生什么


请注意,非被动模式基本上已经过时,因为它不能跨NAT防火墙使用,所以您可能应该将vsFTP配置为允许被动模式。

这很有效!谢谢你,吉姆。但是为什么python ftplib不能自动使用pasive模式呢?我的vsFTP配置为确实允许被动模式。这可能是vsFTP中的配置错误吗?我已经编辑了我的问题帖子,并在那里添加了我的vsftpd.conf。问题不在ftplib中,而是在vsFTP中,出于某种原因,它不允许被动模式。你确定vsFTP配置在正确的位置吗?是的,吉姆,这是vsFTP配置的问题。最后我找到了原因:我的vsFTP部署在NAT中。因此,即使我已经设置了vsFTP选项“pasv_enable=YES”,我也应该设置另一个选项“pasv_address=my.external.ip.address”。在那之后,一切都好了!谢谢你们,感谢热心的社区!这个问题/答案也有帮助