Key takeaways
- subprocess.run can call the curl binary from Python; pass arguments as a list and avoid shell=True.
- Rewriting the call with requests removes the dependency on curl and gives you status codes, JSON parsing and exceptions.
- The free AyeTab cURL to Code converter turns a curl command into requests code in your browser without uploading it.
You can run a curl command in Python with the standard subprocess module, but for most scripts the better move is to rewrite the call with the requests library. The free AyeTab curl converter does that rewrite for you in the browser, so you skip shelling out without retyping a single header.
Both routes work. They just fail in different ways, and picking the right one saves you a confusing afternoon.
Option 1: shell out with subprocess
If you need curl itself, call the binary and capture what it prints:
import subprocess
result = subprocess.run(
["curl", "-s", "https://api.example.com/status",
"-H", "Accept: application/json"],
capture_output=True, text=True, timeout=15,
)
print(result.returncode, result.stdout)
Pass the arguments as a list, not one string with shell=True. A list keeps quotes and spaces intact and avoids the shell injection problems the Python docs warn about in their security notes.
This route has real costs. The script now depends on curl being installed and on the same version everywhere it runs. Errors arrive as an exit code and a blob of text, not as exceptions. And every response is a string you have to parse yourself.
If you stay with subprocess, two small habits help. Add curl's --fail flag so an HTTP error such as a 404 gives a nonzero exit code instead of a quiet success, and pass check=True to subprocess.run so that exit code raises an exception in Python. Without both, a broken endpoint can look like an empty but healthy reply.
Option 2: rewrite it with requests
requests is the usual HTTP library for Python. The same call looks like this:
import requests
response = requests.get("https://api.example.com/status",
headers={"Accept": "application/json"}, timeout=15)
print(response.status_code, response.json())
Status codes, headers, JSON parsing and exceptions all come built in. Nothing outside Python needs to exist on the machine.
Let AyeTab write the rewrite
Translating a long curl command by hand is where mistakes creep in. Paste the command into AyeTab's cURL to Code page, flip the toggle to Python, and copy the requests version. It reads the URL, the -X method, each -H header and the -d body, and it handles commands broken over several lines. Then add a timeout and you are done.
Because it runs in your browser and uploads nothing, it is safe to paste the command you just copied out of dev tools, session cookie and all. That matters more than people admit.
Which one to use
| Situation | Use |
|---|---|
| A normal API call inside a Python app | requests, converted with AyeTab |
| You must match curl's exact behavior or flags | subprocess |
| Porting an existing shell script quickly | subprocess first, requests later |
| Uploads, auth or flags a quick converter skips | requests, using curlconverter or the requests docs |
The honest exception: if a command uses curl options that have no simple requests equivalent, or you are debugging a difference between curl and your code, shelling out to the real binary is the more truthful test. curlconverter is also worth a look for heavy commands, since it parses more of curl's options.
Keep it one shortcut away
AyeTab is a free Chrome extension that turns your new tab into a developer toolbox, with the same tools on the web. Press Ctrl+K, type "curl", and the converter opens next to the JSON formatter and the decoders. Get it from ayetab.com.
Frequently asked questions
How do I capture curl output in Python?
Call subprocess.run with capture_output=True and text=True, then read result.stdout. Check result.returncode, or pass check=True so a failure raises an exception.
Is requests better than calling curl from Python?
For most scripts, yes. It needs nothing outside Python and returns structured responses. Keep curl when you must match its exact behavior.
How do I convert a curl command to Python code?
Paste it into the AyeTab cURL to Code page, choose Python, and copy the requests code it writes.
Sources
- cURL to Code converterAyeTab
- AyeTabAyeTab
- subprocess: subprocess managementPython Software Foundation
- Requests: HTTP for HumansRequests
- curl man pagecurl
- curlconvertercurlconverter