Python3 uses the requests module to send an interface request to the INFLUXDB HTTP API to write data, as follows:
1. Create a database
[python] view plain copy
Import Requests
-
posturl = ' http://192.168.220.128:8086/query '
data = {' q ': ' Create DATABASE mydb '}
Response = Requests.post (PostURL, Data=data)
[python] view plain copy
Print (Response.status_code)
Print (response.headers)
The following are similar to the Curl command:
[python] view plain copy
curl-post/HTTP192.168.220.128:8086/query--data-urlencode"Q=create DATABASE mydb"
2. Writing data to the library
[HTML] view plain copy
Import requests
-
posturl ' http://192.168.220.128:8086/write?db=mydb '
Data = "Cpu_load_short,host=server01,region=us-west value=0.69"
Response = Requests. Post (PostURL,Data=Data)
[HTML] view plain copy
-
print (Response.status_code)
-
print (response.headers)
The following are similar to the Curl command:
[python] view plain copy
curl-i-xpost ' Http://192.168.220.128:8086/write?db=mydb ' --data-binary ' cpu_load_short,host=server01,region=us-west value=0.64 '
Python script writes data to Influxdb