0

I want to execute a curl with command in python.

Usually, I just need enter the command in terminal and press return key.

The command shows below:

curl -H "`oauth2l header --json key.json mobileinsights`"  https://mobileinsights.googleapis.com/v2/networks

The result is in json format.

LeoL
  • 1
  • 2
  • Possible duplicate of [how to use python to execute a curl command](https://stackoverflow.com/questions/25491090/how-to-use-python-to-execute-a-curl-command) – FabienP May 09 '18 at 20:35
  • What's your question? – Klaus D. May 09 '18 at 20:35
  • You could use `subprocess` to run `oauth2l header --json key.json mobileinsights` from Python... Then use whatever HTTP library in python to pass a header – OneCricketeer May 09 '18 at 20:39

2 Answers2

1

Use the subprocess module to run your shell command.

import subprocess
result = subprocess.check_output('curl -H "`oauth2l header --json 
key.json mobileinsights`" https://mobileinsights.googleapis.com/v2/networks', shell=True)

Then, use the json module to parse the JSON data returned by the server.

import json
result_json = json.loads(result) 
Wesley Smith
  • 149
  • 1
  • 9
0

You may load an OS and JSON load modules and then run os.execute(curl URL) store it in any variable then convert it into JSON format with JSON load module