简体   繁体   English

我如何使用 .cer 文件作为 python 请求的一部分

[英]How do I use a .cer file as part of a python request

Say that I have a cer file called symantec-class3.cer, this is what I'm trying:假设我有一个名为 symantec-class3.cer 的 cer 文件,这就是我正在尝试的:

headers = {"content-type": "application/json"}
api_url = "https://www.someurl.com/search"
pprint.pprint(requests.post(api_url, auth=HTTPBasicAuth(username, password), verify="symantec-class3.cer", data=json.dumps(payload), headers=headers).json())

This is not liking it and spews out the following error:这不喜欢它并产生以下错误:

requests.exceptions.SSLError: [Errno 0] _ssl.c:344: error:00000000:lib(0):func(0):reason(0)

It works fine if I turn verify False, but I'm trying to use a cert file in my local directory.如果我将验证设置为 False,它可以正常工作,但我正在尝试在本地目录中使用证书文件。

Python expect cert in PEM format. Python 期望 PEM 格式的证书。

So you should extract the public key component from the X509 certificate using the openssl x509 command.因此,您应该使用 openssl x509 命令从 X509 证书中提取公钥组件。

.cer extension often means a binary DER format, so this command should extract the public key in a form that can be used by pycrypto: .cer 扩展名通常表示二进制 DER 格式,因此此命令应以 pycrypto 可以使用的形式提取公钥:

openssl x509 -inform der -pubkey -noout -in you.cer >public_key.pem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM