Fix Python – Execute curl command within a Python script


Asked By – Kiran Vemuri

I am trying to execute a curl command within a python script.

If I do it in the terminal, it looks like this:

curl -X POST -d  '{"nw_src": "", "nw_dst": "", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}' http://localhost:8080/firewall/rules/0000000000000001

I’ve seen recommendations to use pycurl, but I couldn’t figure out how to apply it to mine.

I tried using:[

and it works, but is there a better way?

Now we will see solution for issue: Execute curl command within a Python script



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('')

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 =, 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.

This question is answered By – OJFord

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0