I have the following command that I run using curl in linux.
curl --tlsv1.2 --cert ~/aws-iot/certs/certificate.pem.crt --key ~/aws-iot/certs/private.pem.key --cacert ~/aws-iot/certs/root-CA.crt -X GET https://data.iot.us-east-1.amazonaws.com:8443/things/pi_3/shadow
This command returns JSON text that I want. However I want to be able to run the above command in Python3. I do not know what library to use in order to get the same JSON response.
P.S. I replace "data" with my account number in AWS to get JSON
After playing around with it on my own I was able to successfully do it in python using the requests library.
import requests
s = requests.Session()
r = s.get('https://data.iot.us-east-1.amazonaws.com:8443/things/pi_3/shadow',
cert=('/home/pi/aws-iot/certs/certificate.pem.crt', '/home/pi/aws-iot/certs/private.pem.key', '/home/pi/aws-iot/certs/root-CA.crt'))
print(r.text)
Related
Is there an api or other way to programmatically run a Databricks job. Ideally, we would like to call a Databricks job from a notebook. Following just gives currently running job id but that's not very useful:
dbutils.notebook.entry_point.getDbutils().notebook().getContext().currentRunId().toString()
To run a databricks job, you can use Jobs API. I have a databricks job called for_repro which I ran using the 2 ways provided below from databricks notebook.
Using requests library:
You can create an access token by navigating to Settings -> User settings. Under Access token tab, click generate token.
Use the above generated token along with the following code.
import requests
import json
my_json = {"job_id": <your_job-id>}
auth = {"Authorization": "Bearer <your_access-token>"}
response = requests.post('https://<databricks-instance>/api/2.0/jobs/run-now', json = my_json, headers=auth).json()
print(response)
The <databricks-instance> value from the above code can be extracted from your workspace URL.
Using %sh magic command script:
You can also use magic command %sh in your python notebook cell to run a databricks job.
%sh
curl --netrc --request POST --header "Authorization: Bearer <access_token>" \
https://<databricks-instance>/api/2.0/jobs/run-now \
--data '{"job_id": <your job id>}'
The following is my job details and run history for reference.
Refer to this Microsoft documentation to know all other operations that can be achieved using Jobs API.
I would like to know how to run the following cURL request using python (I'm working in Jupyter notebook):
curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={your-access-token}"
I've seen some similar questions and answers suggesting using "requests.get", but I am a complete python newbie and am not sure how to structure the syntax for whole request including the id, secret and token elements. Any help would be really appreciated.
Thanks!
no need to use curl. use the below
import requests
graph_api_version = 'a'
app_id = 'b'
app_secret = 'c'
your_access_token = 'd'
url = f"https://graph.facebook.com/{graph_api_version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={your_access_token}"
r = requests.get(url)
Convert your Curl request to Python instantly here :
https://curl.trillworks.com/
you can use some lib that run commands in python like subprocess. for example: subprocess.run(["curl", "google.com"])
This is the requirement from the API's documentation
A HTTPS connection is needed to use the API. This means that you will require a secure SSL/TLS connection to be able to communicate with our API Server.
This is the Curl command of getting the clients in their documentation
curl -i -X GET -H "X-KEYALI-API:{API_KEY}" -u {API_USERNAME}:{API_PASSWORD} https://aliphia.com/v1/api_public/clients/
So, I need to implement the same thing in Python
import requests
headers = {
'X-KEYALI-API': '{API_KEY}',
}
response = requests.get('https://aliphia.com/v1/api_public/clients/', headers=headers, auth=('{API_USERNAME}', '{API_PASSWORD}'))
Currently, I use Powershell to clear cache using the following command:
Invoke-WebRequest https://url -method DELETE
How can I make my Python script do to the same type of delete web request?
This seemed to work:
import requests
url = "https://myurl"
response = requests.delete(url)
how would i tranfoms this curl command:
curl -v -u 82xxxxxxxxxxxx63e6:api_token -X GET https://www.toggl.com/api/v6/time_entries.json
into urlib2?
I found this tutorial: http://www.voidspace.org.uk/python/articles/authentication.shtml
but they use a password and username. I can only use an API token.
Thank you.
see also this question:
Urllib2 raises 403 error while the same request in curl works fine
urllib2 is known to suck big time when it comes to authentication. To save some kitten lifes you should use http://docs.python-requests.org/en/latest/index.html