我有这个curl命令我使用shell脚本:
curl -X POST -H 'Content-Type: application/json' \
--cert /etc/puppetlabs/puppet/ssl/certs/${PEFQDN}.pem \
--key /etc/puppetlabs/puppet/ssl/private_keys/${PEFQDN}.pem \
--cacert /etc/puppetlabs/puppet/ssl/certs/ca.pem \
--data "{ \"name\": \"$GROUP\", \"parent\": \"00000000-0000-4000-8000-000000000000\", \"environment\": \"production\", \"classes\": { \"$CLASS\": {}} }" \
https://${PEFQDN}:4433/classifier-api/v1/groups | python -m json.tool
我想摆脱我的shell脚本,因为我需要能够处理从REST API返回的一些复杂的json。我想我会使用python
和requests
包。我正在查看此文档:
http://docs.python-requests.org/en/latest/user/quickstart/
但我没有看到我可以在哪里指定我的证书信息。在我的shell脚本中,我可以传递curl
这些选项:--cert
,--key
,--cacert
。我不知道如何使用python的requests
API完成相同的操作。有谁知道我怎么能做到这一点?
更新:谢谢Joran。这是我尝试编写一个python脚本来执行此操作:
#!/usr/bin/env python
import requests
# curl https://${PEFQDN}:4433/classifier-api/v1/groups \
# -H "Content-Type: application/json" \
# --cert /etc/puppetlabs/puppet/ssl/certs/${PEFQDN}.pem \
# --key /etc/puppetlabs/puppet/ssl/private_keys/${PEFQDN}.pem \
# --cacert /etc/puppetlabs/puppet/ssl/certs/ca.pem | python -m json.tool
url='https://my-pm.example.com:4433/classifier-api/v1/groups'
headers = {"Content-Type: application/json"}
data={}
cacert='/etc/puppetlabs/puppet/ssl/certs/ca.pem'
key='/etc/puppetlabs/puppet/ssl/private_keys/my-pm.example.com.pem'
cert='/etc/puppetlabs/puppet/ssl/certs/my-pm.example.com.pem'
result = requests.get(url,
data=data, #whatever data
headers=headers, #dict {"Content-Type":"application/json"}
verify=cert,
cert=(cacert,key) #key/cert pair
)
print result.json()
我必须做错事,因为这是我得到的输出......
# ./add-group.py
Traceback (most recent call last):
File "./add-group.py", line 21, in <module>
cert=(cacert,key) #key/cert pair
File "/usr/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg/requests/api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg/requests/sessions.py", line 451, in request
prep = self.prepare_request(req)
File "/usr/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg/requests/sessions.py", line 382, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg/requests/models.py", line 293, in prepare
self.prepare_headers(headers)
File "/usr/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg/requests/models.py", line 401, in prepare_headers
self.headers = CaseInsensitiveDict((to_native_string(name), value) for name, value in headers.items())
AttributeError: 'set' object has no attribute 'items'
我在这里做错了什么?
由于
答案 0 :(得分:3)
headers = {"Content-Type: application/json"}
应改为
headers = {"Content-Type": "application/json"}
看起来请求API期待字典,{&#34; Content-Type:application / json&#34;}显然是Python集。
答案 1 :(得分:2)
QState *s1 = new QState();
QState *s2 = new QState();
machine.addState(s1);
machine.addState(s2);
machine.setInitialState(s1, ifblabla_istrue);
machine.setInitialState(s2, ifblabla_isfalse);
此链接将有所帮助
http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification
答案 2 :(得分:1)
我编写了这个python脚本来执行此操作:
http://redcricketblog.blogspot.com/2015/05/adding-groups-adding-classes-to-group.html