python 如何在 fttplib 中自动添加密码短语

How to automate addition of pass phrase in fttplib in python

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

这个 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)