Tutorials
May 22, 2026
7 min

Python cURL: Run GET, POST and Proxy Requests

Alex Sadovskij
Alex Sadovskij
CEO Proxy-Cheap
Python cURL: Run GET, POST and Proxy Requests
Tóm tắt
Use Python subprocess to run an existing cURL command, capture its response, and check failures. Includes GET, JSON POST, and authenticated proxy examples, plus guidance on credentials, timeouts, and choosing a native Python HTTP client.

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.

Run cURL in Python or convert it to Requests?

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.

ApproachUse it whenWhat you need
subprocess + cURLYou want to run an existing cURL command with its flags.Python standard library and an installed cURL executable.
RequestsYou want native Python response objects, JSON and session handling.Install Requests, then translate the request parameters.
PycURLYou 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.

Convert a cURL request to Python Requests

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 inputRequests equivalent
URL query parametersparams={...}
--header / -Hheaders={...}
JSON Content-Type plus a JSON bodyjson={...}; verify the original payload and content type.
--proxy / --proxy-userA proxies mapping with the correct proxy endpoint and authentication.
--location / -LReview redirect behavior explicitly rather than assuming identical defaults.

Before you start

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.

Run a GET request with subprocess

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.

Send JSON without shell quoting problems

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.

Use a proxy with cURL from Python

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.

Troubleshoot the failing layer

ResultLikely layerNext check
cURL exit 5 or 6Proxy or destination DNS resolutionCheck the hostname and DNS configuration.
cURL exit 7Connection could not be establishedCheck endpoint, port and network reachability.
cURL exit 22 with --fail-with-bodyHTTP response of 400 or aboveInspect the response body/status. A 407 concerns proxy authentication; a 429 concerns request limits.
cURL exit 28cURL timeoutInspect connectivity and response time before increasing timeouts.
cURL exit 60Certificate validationInvestigate the trust chain. Do not routinely disable TLS verification.
Python TimeoutExpiredOuter subprocess timeoutThe 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.

When should you use a Python HTTP client instead?

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.

Choose your next step

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.

References

Câu hỏi thường gặp

To convert cURL to Python, map the HTTP method, URL, headers, request body and authentication to a Python HTTP client such as Requests. Running the command with subprocess is different: it launches the installed cURL executable. Review proxy authentication, timeouts, redirects and TLS settings when converting a request.

Yes. Pass the executable and its arguments as a list to subprocess.run(). Keep shell=False and inspect the exit status before using the response.

Not for subprocess. You need the cURL executable available on PATH. PycURL is a separate option for calling libcurl from Python.