【发布时间】:2017-09-29 14:09:24
【问题描述】:
我正在尝试连接到需要使用 .pfx 证书匿名登录的 FTPS 服务器。
我已获得有关如何通过 gui 应用程序 SmartFTP 访问它的说明,该应用程序确实有效,因此我知道我没有遇到任何防火墙问题等。但是,对于此工作流程,通过 python 访问它是理想的。以下是我得到的设置:
Protocol: FTPS (Explicit)
Host: xxx.xxx.xxx.xxx
Port: 21
login type: Anonymous
Client Certificate: Enabled (providing a .pfx file)
Send FEAT: Send before and after login
我无法通过使用 .pfx 证书的完整示例来选择最适合此的 python 模块。目前我只使用下面的代码尝试了标准的 FTP 模块。有没有人有一个有效的例子?
from ftplib import FTP_TLS
ftps = FTP_TLS(host='xxx.xxx.xxx.xxx',
keyfile=r"/path/to.pfx"
)
ftps.login()
ftps.prot_p()
ftps.retrlines('LIST')
ftps.quit()
使用上面的代码我得到:
ValueError: certfile must be specified
客户端版本: Ubuntu == 14.04, Python == 3.6.2
更新
认为我与下面的代码更接近了,但出现了一个新错误:
from ftplib import FTP_TLS
import tempfile
import OpenSSL.crypto
def pfx_to_pem(pfx_path, pfx_password):
""" Decrypts the .pfx file to be used with requests. """
with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem:
f_pem = open(t_pem.name, 'wb')
pfx = open(pfx_path, 'rb').read()
p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))
ca = p12.get_ca_certificates()
if ca is not None:
for cert in ca:
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))
f_pem.close()
yield t_pem.name
pfx = pfx_to_pem(r"/path/to.pfx", 'password')
ftps = FTP_TLS(host='xxx.xxx.xxx.xxx',
context=pfx
)
ftps.login()
ftps.prot_p()
# ftps.prot_c()
print(ftps.retrlines('LIST'))
ftps.quit()
错误: ftplib.error_perm: 534 服务器上的本地策略不允许 TLS 安全连接。
有什么想法吗?
干杯
【问题讨论】:
标签: ubuntu-14.04 python-3.6 ftps