5
我在Debian中安裝了vsFTP。使用ftp命令手動上傳文件時,沒關係。也就是說,下一屆會議的工作:FTP上傳文件手動工作,但使用Python ftplib失敗
[email protected]:~$ 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以編程方式上傳文件,失敗。錯誤說「超時」,如下面的會話顯示:
[email protected]:~$ 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
這是行得通!謝謝你,吉姆。但爲什麼pypl ftplib不能自動使用pasive模式?我的vsFTP IS配置爲確實允許被動模式。這可能是vsFTP中的配置錯誤嗎?我編輯了我的問題,並在那裏添加了我的vsftpd.conf。 – 2010-07-29 02:13:05
問題不在於ftplib,而是在vsFTP中,由於某些原因,它不允許被動模式。你確定vsFTP配置是否在正確的位置? – 2010-07-29 14:55:42
是的,吉姆,這是vsFTP配置的問題。最後我找到了原因:我的vsFTP部署在NAT中。所以,即使我已經設置vsFTP選項「pasv_enable = YES」,我還應該設置另一個選項「pasv_address = my.external.ip.address」。之後,一切都好!謝謝,感謝熱心的社區! – 2010-07-30 12:22:17