

You can run a cURL command in Python with the standard-library subprocess module. This is useful when you already have a working cURL request and want to run it from a script without translating every option into another HTTP library.
This guide shows how to run cURL commands in Python for HTTP requests, including GET, JSON POST, and a proxy connection check. For a native Python approach, use an HTTP client such as Requests instead.
Using the proxy example? Compare datacenter proxy plans if you need a consistent endpoint, or choose a rotating plan when your workflow needs changing exit IPs.
These are different tasks. Running cURL with subprocess launches the installed command-line program. Converting cURL to Python means translating the method, URL, headers, body, and authentication into a Python HTTP client.
| Approach | Use it when | What you need |
|---|---|---|
| subprocess + cURL | You want to run an existing cURL command with its flags. | Python standard library and an installed cURL executable. |
| Requests | You want native Python response objects, JSON and session handling. | Install Requests, then translate the request parameters. |
| PycURL | You need libcurl-specific controls from Python. | Install PycURL and its platform dependencies. Not needed for these subprocess examples. |
If you use a cURL-to-Python converter, remove private cookies and tokens before sharing the command, then review the generated code. Check proxy authentication, timeouts, redirects, and TLS settings rather than assuming the conversion is equivalent.
For example, the cURL request curl --header "Accept: application/json" "https://httpbin.org/get?topic=proxies" maps to the following native Python request. Install Requests with python -m pip install requests.
import requests
response = requests.get(
"https://httpbin.org/get",
params={"topic": "proxies"}, headers={"Accept": "application/json"}, timeout=30,
)
response.raise_for_status()
print(response.json())
| cURL input | Requests equivalent |
|---|---|
| URL query parameters | params={...} |
| --header / -H | headers={...} |
| JSON Content-Type plus a JSON body | json={...}; verify the original payload and content type. |
| --proxy / --proxy-user | A proxies mapping with the correct proxy endpoint and authentication. |
| --location / -L | Review redirect behavior explicitly rather than assuming identical defaults. |
Install Python 3 and make sure the cURL executable is available. Run curl --version in your terminal; on Windows, use curl.exe --version to avoid a PowerShell alias. Installing a Python package named curl is not required for the subprocess approach.
Pass the executable and each argument as separate list items. Keep the default shell=False; do not build a shell command from user input.
import shutil
import subprocess
curl = shutil.which("curl.exe") or shutil.which("curl")
if not curl: raise SystemExit("Install cURL and make it available on PATH.")
result = subprocess.run(
[curl, "--silent", "--show-error", "--fail-with-body",
"--connect-timeout", "10", "--max-time", "30", "https://httpbin.org/get"],
capture_output=True, text=True, timeout=35,
)
result.check_returncode()
print(result.stdout)
The response body is in result.stdout, diagnostics are in result.stderr, and result.returncode is the process exit status.
The examples use public test endpoints. Use a destination you control for repeatable automated tests. Review diagnostic output for credentials before sharing it.
--fail-with-body requires cURL 7.76.0 or later. If your version does not support it, upgrade cURL or use --fail, which does not preserve an HTTP error response body in the same way.
If cURL exits with an error, check_returncode() raises subprocess.CalledProcessError. If the Python process timeout expires, subprocess.run() raises subprocess.TimeoutExpired. Handle these separately in an application and review captured diagnostics without exposing credentials. A successful process exit is not a substitute for checking the returned content.
Each example includes its own imports and executable lookup. For a POST request, serialize the payload and pass it through standard input. This avoids shell-specific quoting and keeps the payload out of the command arguments.
import json
import shutil
import subprocess
curl = shutil.which("curl.exe") or shutil.which("curl")
if not curl: raise SystemExit("Install cURL and make it available on PATH.")
payload = {"message": "hello", "source": "python"}
result = subprocess.run(
[curl, "--silent", "--show-error", "--fail-with-body",
"--connect-timeout", "10", "--max-time", "30",
"--header", "Content-Type: application/json",
"--data-binary", "@-", "https://httpbin.org/post"],
input=json.dumps(payload), capture_output=True, text=True, timeout=35,
)
result.check_returncode()
print(json.loads(result.stdout)["json"])
Expected JSON for the POST example: {"message": "hello", "source": "python"}. This is an illustrative response shape. The GET example prints the test service’s response document, which includes the requested URL and headers.
For provider-managed rotation, review rotating residential proxy options and confirm the supported protocol and session mode before copying your credentials.
Copy the proxy protocol, host, port, and authentication details from your current Proxy-Cheap dashboard. Test with a small request before adding the proxy to a larger workflow. The following example assumes the endpoint supports HTTP proxy authentication with a username and password.
Supply PROXY_URL, PROXY_USERNAME, and PROXY_PASSWORD through your local environment or secret manager. For example, PROXY_URL would have the form http://HOST:PORT. Never put real credentials in a shared article, screenshot, or repository.
import os
import shutil
import subprocess
curl = shutil.which("curl.exe") or shutil.which("curl")
if not curl: raise SystemExit("Install cURL and make it available on PATH.")
proxy_url = os.environ["PROXY_URL"]
proxy_user = os.environ["PROXY_USERNAME"]
proxy_password = os.environ["PROXY_PASSWORD"]
result = subprocess.run(
[curl, "--silent", "--show-error", "--fail-with-body",
"--connect-timeout", "10", "--max-time", "30",
"--proxy", proxy_url, "--proxy-user", f"{proxy_user}:{proxy_password}",
"https://api.ipify.org"],
capture_output=True, text=True, timeout=35,
)
result.check_returncode()
print("Exit IP:", result.stdout.strip())
Compare the returned address with your expected proxy exit IP. A successful IP check confirms that request worked; it does not guarantee that every destination will accept the connection. Environment variables avoid hardcoding secrets, but --proxy-user still places credentials in process arguments. On shared systems, use a suitably protected cURL configuration or a native HTTP client with your organization’s secret-handling controls.
For SOCKS5 endpoints, use the scheme supported by your product. In cURL, socks5h:// resolves destination hostnames through the proxy, whereas socks5:// resolves them locally. Do not assume an HTTP port also supports SOCKS5.
Need an endpoint for this example? Compare Proxy-Cheap rotating residential plans if your workflow needs provider-managed IP rotation. Choose a suitable session mode, then run the exit-IP check above before adding more requests.
| Result | Likely layer | Next check |
|---|---|---|
| cURL exit 5 or 6 | Proxy or destination DNS resolution | Check the hostname and DNS configuration. |
| cURL exit 7 | Connection could not be established | Check endpoint, port and network reachability. |
| cURL exit 22 with --fail-with-body | HTTP response of 400 or above | Inspect the response body/status. A 407 concerns proxy authentication; a 429 concerns request limits. |
| cURL exit 28 | cURL timeout | Inspect connectivity and response time before increasing timeouts. |
| cURL exit 60 | Certificate validation | Investigate the trust chain. Do not routinely disable TLS verification. |
| Python TimeoutExpired | Outer subprocess timeout | The Python time budget expired. Handle it separately from cURL’s exit status. |
Use our proxy testing guide and proxy troubleshooting checklist to isolate connection problems. Avoid sharing raw stderr or verbose logs until you have checked them for credentials and private request data.
Use subprocess when reproducing an existing cURL command matters. For an application making many requests, a Python HTTP client is usually easier to maintain because you can work directly with sessions, response objects, and application-level error handling. For rotation workflows, continue with our Python proxy rotation guide.
Once the connection check succeeds, choose a datacenter proxy plan for a consistent endpoint, or use the rotating residential option above when rotation is part of your workflow. Start with a small test before increasing request volume.