asm*_*ier 0 python ftp curl ftplib
我设法使用 curl 连接到 FTP 服务器并列出目录的内容out:
$ curl -v --insecure --ftp-ssl --user xxx:yyy blabla:990/out/
> AUTH SSL
< 234 Proceed with negotiation.
...
> USER xxx
< 331 Please specify the password.
> PASS yyy
< 230 Login successful.
> PBSZ 0
< 200 PBSZ set to 0.
> PROT P
< 200 PROT now Private.
> PWD
< 257 "/"
> CWD out
< 250 Directory successfully changed.
> EPSV
< 229 Entering Extended Passive Mode (|||51042|).
* Trying aaa.bbb.ccc.ddd...
* Connecting to aaa.bbb.ccc.ddd (aaa.bbb.ccc.ddd) port 51042
* Connected to blabla (aaa.bbb.ccc.ddd) port 990 (#0)
> TYPE A
< 200 Switching to ASCII mode.
> LIST
< 150 Here comes the directory listing.
* Maxdownload = -1
* Doing the SSL/TLS handshake on the data stream
* SSL re-using session ID
* TLS 1.0 connection using TLS_RSA_WITH_AES_256_CBC_SHA
* Server certificate: blabla
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
{ [539 bytes data]
100 539 0 539 0 0 900 0 --:--:-- --:--:-- --:--:-- 899* Remembering we are in dir "out/"
< 226 Directory send OK.
Run Code Online (Sandbox Code Playgroud)
我尝试使用 python(Mac OS X 10.10.5 上的 2.7.10)做同样的事情
import ftplib
ftps = ftplib.FTP_TLS()
ftps.set_debuglevel(1)
print ftps.set_pasv(True)
print ftps.connect("blabla", 990)
print ftps.login("xxx", "yyy")
print ftps.sendcmd("PBSZ 0")
print ftps.prot_p()
print ftps.pwd()
print ftps.cwd("out")
print ftps.transfercmd("LIST")
ftps.close()
Run Code Online (Sandbox Code Playgroud)
但是使用 python 会LIST command无限期地挂起
*cmd* 'AUTH TLS'
*resp* '234 Proceed with negotiation.'
*cmd* 'USER xxx'
*resp* '331 Please specify the password.'
*cmd* 'PASS ********\n'
*resp* '230 Login successful.'
230 Login successful.
*cmd* 'PBSZ 0'
*resp* '200 PBSZ set to 0.'
200 PBSZ set to 0.
*cmd* 'PBSZ 0'
*resp* '200 PBSZ set to 0.'
*cmd* 'PROT P'
*resp* '200 PROT now Private.'
200 PROT now Private.
*cmd* 'PWD'
*resp* '257 "/"'
/
*cmd* 'CWD out'
*resp* '250 Directory successfully changed.'
250 Directory successfully changed.
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (aa,bb,cc,dd,199,97).'
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?
更新:好的,我在--disable-epsvcurl 命令中添加了该选项,但这也失败了。所以python无法列出目录的原因是使用PASV. 如何强制使用 Python EPSV?
UPDATE2:这个bug https://github.com/python/cpython/pull/28好像是PASV返回的IP地址错误时导致Python ftplib挂掉的。这似乎也是我的问题。但是有人知道如何强制使用 Python 的解决方法EPSV吗?
通过阅读我认为的ftplib的源代码,可以EPSV通过设置强制 ftplib 使用
ftps.af = socket.AF_INET6
Run Code Online (Sandbox Code Playgroud)
这使得FTPLIB认为,我们使用的是IPv6的连接(即使我们在使用IPv4的事实),并使其使用EPSV替代PASV。我的完整工作计划结束了
import ftplib
import socket
ftps = ftplib.FTP_TLS()
ftps.connect("blabla", 990)
ftps.login("xxx", "yyy")
ftps.prot_p()
ftps.cwd("out")
ftps.af = socket.AF_INET6
ftps.retrlines("LIST")
ftps.quit()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2369 次 |
| 最近记录: |