【问题标题】:Python-Requests receive client private key as stringPython-Requests 接收客户端私钥作为字符串
【发布时间】:2021-03-24 01:09:19
【问题描述】:

我的问题

我在使用python requests 时尝试使用加密的客户端私钥。由于它不支持加密密钥,我必须先将其解密,然后将其发送至requests。问题是requests 只接受用于指定客户端密钥/证书的文件路径。我怎样才能让它接受一个字符串?

我想做的例子:

#X509
CLIENT_CERT_PATH = "path/to/cert" #perhaps I should convert this into a string too?

#PKey
CLIENT_PKEY_STRING = decrypt("path/to/encrypted_key")

response = requests.get(url = url, cert = (CLIENT_CERT_PATH, CLIENT_PKEY_STRING)) #how?

尝试

尝试了Python requests CA certificates as a string,但我得到了以下行为:AttributeError: module 'urllib3.contrib' has no attribute 'pyopenssl'

还尝试了Example of including a certficate in a post request with python and http.client,甚至将其修改为:

class openSSLContext(requests.packages.urllib3.util.ssl_.SSLContext):
    def __init__(self, method):
        pk = crypto.load_privatekey(crypto.FILETYPE_PEM, open(CL_PKEY_PATH, "rb").read())
        cert = crypto.load_certificate(crypto.FILETYPE_PEM, open(CL_CERT_PATH, "rb").read())
        

requests.packages.urllib3.util.ssl_.SSLContext = openSSLContext

但我不知道从这里去哪里。

版本

Python 3.9.2
pyOpenSSL == 20.0.1
requests == 2.25.1
urllib3 == 1.26.4

【问题讨论】:

    标签: python http ssl https python-requests


    【解决方案1】:

    编辑:下面的代码不起作用

    如果你想修改下面给出的第二个例子,你应该这样做

    class openSSLContext(requests.packages.urllib3.util.ssl_.SSLContext):
        def __init__(self, method):
            pk = crypto.load_privatekey(crypto.FILETYPE_PEM, open("./key.pem", "rb").read())
            cert = crypto.load_certificate(
                crypto.FILETYPE_PEM, open("./cert.pem", "rb").read()
            )
            self.certfile = cert
            self.keyfile = pk
    
    
    requests.packages.urllib3.util.ssl_.SSLContext = openSSLContext
    

    【讨论】:

    • 我将如何修改第二个 awnser(我试图适应的那个)?
    • 很遗憾没用 :( 编辑了错误的帖子
    • 您忘记在__init__super.__init__ 中传递方法参数
    • 已经解决了这个问题。我现在得到一个不同的错误
    • 这是一个补丁和糟糕的代码,尝试通过 OpenSSL.SSL.TLSv1_2_METHOD 而不是 method in super().__init__
    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 2018-08-10
    • 2017-08-27
    • 2017-04-11
    相关资源
    最近更新 更多