python - 如何在python中的ftplib中自动添加密码短语

标签 python ssl curl ftp ftps

我正在尝试使用 ftps 下载文件。我在 python 中使用 ftplib。问题是在尝试建立连接时,我总是必须手动输入 key 的密码。我怎样才能使它自动化?

这个 curl 命令运行良好

curl -k -la --ftp-method nocwd --ftp-ssl -u test_user:test_password --key my_key.key --cert my_cert.crt:cert_password ftp.mysite.com

但是当我尝试使用 python 时,我必须手动输入“cert_password”

import ftplib
conn = ftplib.FTP_TLS(host='ftp.mysite.come', user='test_user', passwd='test_password', keyfile='my_key.key', certfile='my_cert.crt') 

最佳答案

我没有使用 ftplib,但根据 [Python]: class ftplib.FTP_TLS(host='', user='', passwd='', acct='', keyfile=None, certfile=None, context=None, timeout=None, source_address=None) :

keyfile and certfile are a legacy alternative to context – they can point to PEM-formatted private key and certificate chain files (respectively) for the SSL connection.

查看[Python]: SSL Contexts ,您应该分两步进行连接:

import ssl
import ftplib
ctx = ssl.SSLContext()
ctx.load_cert_chain("my_cert.crt", keyfile="my_key.key", password="cert_password")
conn = ftplib.FTP_TLS(host="ftp.mysite.come", user="test_user", passwd="test_password", context=ctx)

关于python - 如何在python中的ftplib中自动添加密码短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50160364/

相关文章:

python - 如何通过引用 pandas 中具有列类型详细信息的其他表来更改列数据类型?

python - 字符串整数必须是整数 JSON 列表

python - Python 的 ruby​​ 代码的等效 HMAC SHA512 key

servlets - 带有 Jetty 的 SSL 内置到 EC2 上的 Jar 中

php - Laravel 5.2 Socialite Facebook 登录 cURL 错误 60

c++ - 如何正确地将 libcurl 连接到 Visual Studio 2012?

linux - 通过linux命令向端口发送数据

python - 为什么带有自定义绘画的小部件不可见?

c# - 如何在 CentOS 上的 .Net-Core 2 中忽略 SSL 证书验证

php - CURLOPT_FILE、curl_multi_exec 和 fclose