如何在urllib中使用主动FTP模式?

时间:2014-02-19 10:50:33

标签: python python-2.7 urllib

问题很简单:

>>> urllib.urlretrieve('ftp://maia.usno.navy.mil/ser7/tai-utc.dat', 'tai-utc.dat')
...
IOError: [Errno ftp error] [Errno ftp error] 502 Command not implemented: PASV

显然urllib在默认模式下使用ftplib,这意味着被动模式,被企业防火墙阻止。如果被动失败,ftplib就不够聪明,无法尝试主动模式。

使用wget:

$ wget ftp://maia.usno.navy.mil/ser7/tai-utc.dat
--2014-02-19 11:26:31--  ftp://maia.usno.navy.mil/ser7/tai-utc.dat
           => `tai-utc.dat'
Resolving maia.usno.navy.mil (maia.usno.navy.mil)... 199.211.133.23
Connecting to maia.usno.navy.mil (maia.usno.navy.mil)|199.211.133.23|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD (1) /ser7 ... done.
==> SIZE tai-utc.dat ... 3159
==> PASV ... 
Cannot initiate PASV transfer.
==> PORT ... done.    ==> RETR tai-utc.dat ... done.
Length: 3159 (3.1K) (unauthoritative)

100%[==================================================================================================>] 3,159       --.-K/s   in 0s      

2014-02-19 11:26:32 (211 MB/s) - `tai-utc.dat' saved [3159]

如何在urllib中使用主动模式?如果不可能,我有什么选择?

0 个答案:

没有答案