How to use Python to execute a cURL command?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you already have a working curl command and want to call it from Python, the direct solution is to run the command with subprocess. In many cases, though, the better solution is to skip curl entirely and make the HTTP request with a Python library such as requests.
The right choice depends on why curl is involved. Use subprocess when you truly need the curl executable or want to reuse an existing shell command. Use requests when your goal is simply to send HTTP traffic from Python code.
Execute curl Safely with subprocess
The safest standard-library approach is subprocess.run with a list of arguments. Passing a list avoids shell-quoting problems and reduces injection risk.
Key options:
- '
capture_output=Truecollects standard output and standard error' - '
text=Truereturns decoded strings instead of bytes' - '
check=Trueraises an exception if the command exits with a non-zero status'
If curl is not installed or not on your PATH, Python raises FileNotFoundError.
Pass Headers, Data, and Authentication
The same pattern works for POST requests:
Notice that the JSON body is passed as one argument. That is another reason to prefer argument lists over manually building one long shell string.
Avoid os.system
You may see examples using os.system, but it gives you poor error handling and makes quoting harder:
This works for simple cases, but you do not get structured access to output, and failures are harder to diagnose. For new code, subprocess.run is the right default.
Prefer Native HTTP in Python When Possible
If the goal is just to call an API, the requests library is usually cleaner:
This removes the external dependency on curl, gives you native access to status codes and JSON parsing, and keeps your code cross-platform.
A good rule is:
- use
subprocessif you already depend on a specificcurlinvocation - use
requestsif you are writing normal Python application code
Capture Errors Clearly
When you call curl through subprocess, capture both standard output and standard error so failures are visible:
That gives you enough information to distinguish DNS failures, TLS errors, or HTTP-related command options problems.
Common Pitfalls
- Building one shell string from untrusted input instead of passing an argument list to
subprocess.run. - Using
os.systemand then struggling to capture output or diagnose failures. - Assuming
curlexists on every machine where the Python script runs. - Using
curlfrom Python when a native HTTP library would be simpler and easier to maintain.
Summary
- Use
subprocess.runwith a list of arguments when you need to executecurlfrom Python. - Capture output and enable
check=Trueso failures are visible and actionable. - Avoid
os.systemfor anything beyond trivial experiments. - Prefer
requestswhen you only need to send HTTP requests and do not specifically need thecurlexecutable. - Choose the approach that matches the actual dependency in your project instead of wrapping shell commands by habit.
Related reading
- How to use Python type hints with Django QuerySet?
- How to use raise keyword in Python
- How to use request_id while logging in asynchronous functions?
- How to use string.replace in python 3.x
- How to use subprocess command with pipes
- How to use TensorFlow in OOP style?
- How to use TensorFlow metrics in Keras
- How to use tensorflow on spyder?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.