How to Use Python to Execute a Curl Command

Execute curl command within a Python script

Don't!

I know, that's the "answer" nobody wants. But if something's worth doing, it's worth doing right, right?

This seeming like a good idea probably stems from a fairly wide misconception that shell commands such as curl are anything other than programs themselves.

So what you're asking is "how do I run this other program, from within my program, just to make a measly little web request?". That's crazy, there's got to be a better way right?

Uxio's answer works, sure. But it hardly looks very Pythonic, does it? That's a lot of work just for one little request. Python's supposed to be about flying! Anyone writing that is probably wishing they just call'd curl!



it works, but is there a better way?

Yes, there is a better way!

Requests: HTTP for Humans

Things shouldn’t be this way. Not in Python.

Let's GET this page:

import requests
res = requests.get('https://stackoverflow.com/questions/26000336')

That's it, really! You then have the raw res.text, or res.json() output, the res.headers, etc.

You can see the docs (linked above) for details of setting all the options, since I imagine OP has moved on by now, and you - the reader now - likely need different ones.

But, for example, it's as simple as:

url     = 'http://example.tld'
payload = { 'key' : 'val' }
headers = {}
res = requests.post(url, data=payload, headers=headers)

You can even use a nice Python dict to supply the query string in a GET request with params={}.

Simple and elegant. Keep calm, and fly on.

How to execute a curl command in Python and get the response and pass it through other code

Like avloss said - try out requests.

Another great resource is the application Postman

It will let you try out any http calls you'd like, and then can also translate that into/out of curl and/or python requests code for you (and a bunch of other languages).

I find it really useful when trying to figure how to use requests for anything more than simple http calls.

Using Curl command within a Python script

You should consider doing this in native Python, rather than executing curl externally. Here's an example of how to make a POST request using Python and the requests package:

import requests
import json
response = requests.post('https://yoururl', data = {'key':'value'})
with open('output.json', 'w') as f:
json.dump(response.json(), f)

You can read the requests documentation to read/write headers etc.

And for your specific case:

import requests
import json

headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}

data = {
"Products": [],
"DataProducts": [],
"includeExtractFields": True,
"includedDocumentTypes": [],
"removeOrphans": True,
"searchDataProduct": "Model|test",
"searchField": "ID_TEST",
"searchValues": [ "123456789","987654321" ]
}

# To send data form-encoded
response = requests.post('http://test/', headers=headers, data=data)

# To send data json-encoded
response = requests.post('http://test/', headers=headers, json=data)

# Save response as JSON file
with open('output.json', 'w') as f:
json.dump(response.json(), f)

Executing curl command in python script

Instead of executing curl command I used the python requests to gather my data for parsing.

import requests
from requests.auth import HTTPBasicAuth

# Making a get request
response = requests.get('https://confluence.ai.com/rest/api/content/494966599?expand=body.storage',
auth=HTTPBasicAuth('svc-Automation@ai.com', 'Roboticsengineering1@ai')).json()

# print request object
print(response)

Execute curl command and fetch response

try this:

import requests

url = 'https://api.abc.com/targets/AxthamE0v3E-/scans/S6dOMPj8SsoH/'
r = requests.get(url,headers={"Content-Type": "application/json","Authorization": "JWT PROBELY_AUTH_TOKEN"})
print(r.text)


Related Topics



Leave a reply



Submit