FTP上传文件手动工作,但使用Python ftplib失败 [英] FTP upload file works manually, but fails using Python ftplib

查看:617
本文介绍了FTP上传文件手动工作,但使用Python ftplib失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Debian中安装了vsFTP。使用ftp命令手动上传文件时,没关系。即,以下会话有效:

 
john @ myhost:〜$ ftp xxx.xxx.xxx.xxx 5111
已连接到xxx.xxx.xxx.xxx。
220您好,欢迎来到我的FTP服务器。
名称(xxx.xxx.xxx.xxx:john):ftpuser
331请指定密码。
密码:
230登录成功。
远程系统类型是UNIX。
使用二进制模式传输文件。
ftp> put st.zip
local:st.zip remote:st.zip
200 PORT命令成功。考虑使用PASV。
150确定发送数据。
226文件接收确定。
12773字节在0.00秒内发送(277191.8 kB / s)
ftp> 221再见。

(请注意,如上所述,我将vsFTP服务器配置为使用非默认端口,例如5111,出于某种原因)



现在,当我在python中编写脚本以编程方式上传文件时,它失败了。如下面的会话所示:

 
john @ myhost:〜$ ipython
Python 2.5。 2(r252:60911,2010年1月24日,14:53:14)
输入copyright,credits或license以获取更多信息。

IPython 0.8.4 - 增强型交互式Python。
? - > IPython功能介绍和概述。
%quickref - >快速参考。
帮助 - > Python自己的帮助系统。
对象? - >关于'对象'的细节。 ?对象也起作用,??打印更多。

在[1]中:import ftplib

在[2]中:ftp = ftplib.FTP()

In [3]:ftp。连接('xxx.xxx.xxx.xxx','5111')
输出[3]:220您好,欢迎来到我的FTP服务器。

在[4]中:ftp.login('ftpuser','ftpuser')
Out [4]:'230 Login successful。'

In [ 5]:f = open('st.zip','rb')

在[6]中:ftp.storbinary('STOR%s'%'my_ftp_file.zip',f)
------------------------------------------------ ---------------------------
错误追溯(最近一次调用最后)

...

/usr/lib/python2.5/ftplib.pyc in ntransfercmd(self,cmd,rest)
322,socktype,proto,canon,sa = socket.getaddrinfo(host, port,0,socket.SOCK_STREAM)[0]
323 conn = socket.socket(af,socktype,proto)
- > 324 conn.connect(sa)
如果rest是不是无:
326 connect(self,* args)$ b中的/usr/lib/python2.5/socket.pyc
$ self.sendcmd(REST%s%rest)


$ b错误:(110,'连接时间d out')

我想我的vsFTP服务器中有一些错误的配置,但无法弄清楚。任何人都可以提供帮助吗?

我的vsFTP配置是:

 

listen =是

connect_from_port_20 =是
listen_port = 5111
ftp_data_port = 5110

#被动FTP模式允许
pasv_enable = YES
pasv_min_port = 5300
pasv_max_port = 5400

max_per_ip = 2


解决方案在您尝试发送数据之前,超时不会发生,因此您可以成功连接到服务器。我看到的唯一区别是ftplib默认使用被动模式,而您的命令行客户端似乎不使用。尝试做

  ftp.set_pasv(False)

,然后看看会发生什么。注意,非被动模式基本上已经过时,因为它不能跨越NAT防火墙,所以你应该配置vsFTP以允许被动模式。


I installed vsFTP in a Debian box. When manually upload file using ftp command, it's ok. i.e, the following session works:

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.

(Please noted that as above, I configured vsFTP server to use a non-default port,e.g 5111 for some reason)

Now when I write a script in python to upload file programmatically, it failed. the error says 'time out', as the following session shows:

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')

I guess there is some wrong config in my vsFTP server, but cannot figure it out. Anyone can help ?

my vsFTP config is:


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

解决方案

The timeout doesn't happen until you try to send the data, so you were able to connect to the server successfully. The only difference I see is that ftplib uses passive mode by default, whereas your command-line client does not appear to. Try doing

ftp.set_pasv(False)

before initiating the transfer and see what happens.

Note that non-passive mode is essentially obsolete because it cannot be used across NAT firewalls, so you should probably configure vsFTP to allow passive mode.

这篇关于FTP上传文件手动工作,但使用Python ftplib失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆