首页 文章

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

提问于
浏览
6

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

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.

(请注意,如上所述,我将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')

我想我的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

1 回答

  • 6

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

    ftp.set_pasv(False)
    

    在开始转移之前,看看会发生什么 .

    请注意,非被动模式基本上已过时,因为它无法跨NAT防火墙使用,因此您应该将vsFTP配置为允许被动模式 .

相关问题