我试图在python中转换这个实际工作的cUrl命令:
curl -i -XPOST 'http://51.255.80.74:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.65'
通过这样做,我通过HTTP API在我的涌入数据库testdb中添加了一个时间系列数据。 我想在我的python脚本中使用请求模块做同样的事情,但是我收到了这个错误:
Traceback (most recent call last):
File "script3.py", line 27, in <module>
response = requests.post(urlPostData, data=data, headers={'Content-Type': 'application/octet-stream'})
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))
我的脚本python是:
import requests
import xml.etree.ElementTree as ET
###
interestingData = ['lps0','wuc0']
###INFLUXDB INFOS
databaseName = 'testdb'
urlPostData = 'http://51.255.80.74:8086/write?db='+databaseName
urlGetData = 'http://192.168.168.110/admin/status.xml'
source = requests.get(urlGetData, stream=True)
tree = ET.parse(source.raw)
root = tree.getroot()
for child in root:
if child.tag in interestingData:
data = ''+child.tag+',host=serverlama,region=us-west value='+child.text
response = requests.post(urlPostData, data=data, headers={'Content-Type': 'application/octet-stream'})
我不知道为什么它不起作用,我想我在python请求中很好地转换了我的cURl。