简体   繁体   English

使用curl转换为具有多个SSL参数的python请求

[英]Convert with curl to python requests with multiple ssl params

I have been asked to write a python script which will send a post request to a secured API which requires a key, and 2 certifications ('cert' and 'cacert'). 我被要求编写一个python脚本,该脚本将向安全的API发送发布请求,该API需要密钥和2个证书(“ cert”和“ cacert”)。 I usually use bash with curl and type it manually like this: 我通常使用带有curl的bash并像这样手动输入它:

curl -d "@data.json" -X POST https://example.com/api/v1/ --cert cert1.crt  --key key.key --cacert cert2.crt

I was trying to convert it to python requests and out all the ssl methods under 'params' like this, it didn't work: 我试图将其转换为python请求,并在“参数”下删除所有ssl方法,如下所示,它不起作用:

data = open('data.json')
params = {'cert': 'cert1.crt',
          'key ': 'key.key',
          'cacert': 'cert2.crt'
          }

response = requests.post('https://example.com/api/v1/',
                             data=data, params=params)

According to the requirements, I must use 'requests' and not 'pycurl'. 根据要求,我必须使用“请求”而不是“ pycurl”。

Anyone can help? 有人可以帮忙吗?

I found the solution, apparently cacert is a bundle certificate that must be verified before sending the request and then I should use the certificate itself and the key. 我找到了解决方案,显然cacert是捆绑证书,必须在发送请求之前对其进行验证,然后再使用证书本身和密钥。

So it looks like this: 所以看起来像这样:

with open('data.json', 'r') as f:
    data = json.loads(f.read())

response = requests.post("https://example.com", json=data,
                         verify="cert2.crt", cert=("cert1.crt", "key.key"))

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

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