How to use Python to execute complex CURL command for InfluxDB - python

Could I anyone explain how I can use requests in Python to send this CURL:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
I dont know how to parse it into a sample code. Thanks a lot

import requests
url_string = 'http://localhost:8086/write?db=mydb'
data_string = 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
r = requests.post(url_string, data=data_string)

To create a InfluxDB via Python3:
#!/usr/bin/env python3
import urllib.request as requests
url = "http://localhost:8086/query"
params = {
"q=CREATE DATABASE mydb"
}
r = requests.Request(url, data=params)

Related

using Requests for a get request instead of curl

I'm trying to play with Petfinder's API, which recommends using curl to get a token key instead of requests. How can I convert their recommended curl code into Requests specific language that works? THe documentations is listed here https://www.petfinder.com/developers/v2/docs/#using-the-api
Recommended curl code:
curl -d "grant_type=client_credentials&client_id={CLIENT-ID}&client_secret={CLIENT-SECRET}" https://api.petfinder.com/v2/oauth2/token
I tried the below (iterating through combos of CLIENT-ID and CLIENT-SECRET, but to no success.
import requests
params = {params = {'CLIENT-ID': XXXXXXXXX,
'CLIENT-SECRET': XXXXXXXX}
}
r = requests.get('https://api.petfinder.com/v2/oauth2/token', params = params)
And I received an error 401 message that indicates that my authorization isn't sufficient.
try like this:
import requests
client_id = ""
client_pass = ""
data = {"grant_type":"client_credentials"}
api_url = "https://api.petfinder.com/v2/oauth2/token"
r = requests.post(api_url,data=data,auth=(client_id,client_pass))
r = r.json()

Callling a REST API in python

I want to call a REST api and get some json data in response in python.
curl https://analysis.lastline.com/analysis/get_completed -X POST -F “key=2AAAD5A21DN0TBDFZZ66” -F “api_token=IwoAGFa344c277Z2” -F “after=2016-03-11 20:00:00”
I know of python request, but how can I pass key, api_token and after? What is -F flag and how to use it in python requests?
Just include the parameter data to the .post function.
requests.post('https://analysis.lastline.com/analysis/get_completed', data = {'key':'2AAAD5A21DN0TBDFZZ66', 'api_token':'IwoAGFa344c277Z2', 'after':'2016-03-11 20:00:00'})
-F stands for form contents
import requests
data = {
'key': '2AAAD5A21DN0TBDFZZ66',
'api_token': 'IwoAGFa344c277Z2',
'after': '2016-03-11',
}
response = requests.post('https://analysis.lastline.com/analysis/get_completed', data=data)
-F means make a POST as form data.
So in requests it would be:
>>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})

creating a request to replace curl -XDelete - Python

I'm executing the following cURL command to clean an Elastic Search index:
curl -s -XDELETE "http://<server>/<server_index>/<index>/_query" -d '{
"query":{"match_all":{}}}
}'
However, I don't to execute a shell script or a subprocess command. Instead, I want to rely on requests to achieve this:
import requests
r = requests.get('http://<server>/<server_index>/<index>/_query')
How can I accomplish this?
The equivalent requests code should be something like:
data = {"query": {"match_all": {}}}
import requests
r = requests.delete("http://<server>/<server_index>/<index>/_query", data=data)

Python Requests Put not working with build.phonegap.com

I am making a python build script for a phonegap project.
I need to open the ios key before i build
I am trying to do this with a http put request through the requests module for python.
If i do it with cURL from command line, it works fine
curl -vvv -d 'data={"password":"myPassWord"}' -X PUT https://build.phonegap.com/api/v1/keys/ios/193686?auth_token=passwordlesstokenphg
But from python like this.
password_for_key = {'password': 'myPassword'}
authentication_token = {'auth_token': 'passwordlesstokenphg'}
requests.put('https://build.phonegap.com/api/v1/keys/ios/193686', data=password_for_key, params=authentication_token)
It just returns the json you would recieve if you did a cURL without the data.
For me it seems like the data is not being sent to phonegap correctly.
API reference from build.phonegap.com
docs.build.phonegap.com/en_US/2.9.0/developer_api_write.md.html
Please help :)
So when you do
curl -d "..." -X PUT https://example.com
curl sends exactly what's in that string. requests does not translate so directly to curl. To do something similar in requests you need to do the following:
import json
password_for_key = {'password': 'myPassword'}
authentication_token = {'auth_token': 'passwordlesstokenphg'}
requests.put('https://build.phonegap.com/api/v1/keys/ios/193686',
data={'data': json.dumps(password_for_key)},
params=authentication_token)
What requests will do is build data={"password":"myPassword"} for you if you use the above. First you have to JSON encode the data in password_for_key then pass it in the dictionary to data.

Convert this curl cmd to Python 3

The following curl command works perfectly (private data anonymized):
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/abc/SMS/Messages.json' \
-d 'From=%2B14155551234' \
-d 'To=%2B17035551212' \
-d 'Body=This+is+a+test' \
-u foo:bar
How can I send out this exact same HTTPS POST request in the proper Python3.3 way? I don't want to have to use anything other than Python 3.3's standard library if I can avoid it (in other words, not using the twilio python module, or "requests", or pycurl, or anything outside the plain vanilla Python 3.3 installation).
The preferred Python approach seems to keep evolving from version to version, the snippets I find by Googling never mention which version they're using or don't do the login part, the Python docs are full of "deprecated since 3.x" but never include code examples of the new way to do things....
If curl can do this so easily, so can standard Python 3.3. But how exactly is this supposed to be done now?
Here's a version that works both on Python 2 and 3:
import requests # pip install requests
url = 'https://api.twilio.com/2010-04-01/Accounts/abc/SMS/Messages.json'
r = requests.post(url, dict(
From='+17035551212',
To='+17035551212',
Body='This is a test'), auth=('foo', 'bar'))
print(r.headers)
print(r.text) # or r.json()
To make https post request with basic http authentication on Python 3.3:
from base64 import b64encode
from urllib.parse import urlencode
from urllib.request import Request, urlopen
user, password = 'foo', 'bar'
url = 'https://api.twilio.com/2010-04-01/Accounts/abc/SMS/Messages.json'
data = urlencode(dict(From='+17035551212', To='+17035551212',
Body='This is a test')).encode('ascii')
headers = {'Authorization': b'Basic ' +
b64encode((user + ':' + password).encode('utf-8'))}
cafile = 'cacert.pem' # http://curl.haxx.se/ca/cacert.pem
response = urlopen(Request(url, data, headers), cafile=cafile)
print(response.info())
print(response.read().decode()) # assume utf-8 (likely for application/json)

Categories