如何将此curl命令转换为pycurl或python请求?

问题描述 投票:0回答:1

非常感谢您。只是无法使它正常工作。我基本上是想通过http请求更新我的Grafana仪表板。设法使它与curl配合使用,但希望通过python的请求或pycurl来做到这一点。

curl -X PUT https://<api token>@ks.hostedgraphite.com/api/v2/grafana/dashboards/<my_dashboard> --data-binary @dashboard.json

以上命令有效。尝试了几种方法,例如代码片段的示例:

crl = pycurl.Curl()
          crl.setopt(crl.URL,
           'https://[email protected]/api/v2/grafana/dashboards/<my_dashborad>')
crl.setopt(crl.UPLOAD, 1)
file = open('dashboard.json')
crl.setopt(crl.READDATA, file)
crl.perform()
crl.close()
file.close()
print('Status: {}'.format(crl.getinfo(crl.RESPONSE_CODE)))
api curl python-requests grafana pycurl
1个回答
0
投票
curl -X PUT https://[email protected]/api/v2/grafana/dashboards/my_dashboard --data-binary @dashboard.json 

翻译为

import requests

data = open('dashboard.json', 'rb').read()
response = requests.put('https://[email protected]/api/v2/grafana/dashboards/my_dashboard', data=data)

只需替换api_tokenmy_dashboard的适当值。

您可以使用https://curl.trillworks.com/使用python requests将curl命令转换为等效代码。

© www.soinside.com 2019 - 2024. All rights reserved.