# Using Core Proxies with Python (Requests) | Core Proxies

Source: https://coreproxies.com/help/article/python/

Last updated: 2026-11-27

## Using Core Proxies with Python (Requests)

The requests library takes a proxy as a URL. This article shows the rotating and sticky forms, SOCKS5, and a small helper with timeouts and retries that you can paste into a project.

November 27, 2026 · 5 min read · Python

### Install requests

```python
python -m pip install requests
```

In the code below, `HOST`, `PORT`, `USERNAME` and `PASSWORD` stand for the values in your dashboard. The rotating and sticky examples follow Residential, Unlimited Residential and Rotating Mobile. For a Datacenter or ISP order, pick rotating, sticky or static in the Proxy generator of the order and pass the generated line to [`to_proxy_url()`](https://coreproxies.com/help/article/python/#string).

### Rotating proxy

Pass the same proxy URL for both the `http` and `https` keys. On Residential and Rotating Mobile, a username without a session gets a new IP with every new connection:

```python
import requests

PROXY = "http://USERNAME:PASSWORD@HOST:PORT"
proxies = {"http": PROXY, "https": PROXY}

r = requests.get(
    "https://api.ipify.org?format=json",
    proxies=proxies,
    timeout=30,
)
print(r.json())  # the exit IP of this connection
```

The proxy URL starts with `http://` even for HTTPS targets. It describes how you talk to the proxy; the connection to the target site is still encrypted end to end through a `CONNECT` tunnel.

### Sticky session

To keep one IP across several requests, add a session number and a TTL in seconds to the username: `USERNAME-session-482913-ttl-600` keeps one IP for 600 seconds. On Residential and Unlimited Residential a sticky session lasts up to 24 hours. A `requests.Session` keeps cookies and reuses connections, which fits a multi-step flow:

```python
import random
import requests

ENDPOINT = "HOST:PORT"
USER = "USERNAME"
PASSWORD = "PASSWORD"

def sticky_proxy(ttl_seconds=600):
    session_id = random.randint(100000, 999999)
    return f"http://{USER}-session-{session_id}-ttl-{ttl_seconds}:{PASSWORD}@{ENDPOINT}"

proxy = sticky_proxy()
s = requests.Session()
s.proxies = {"http": proxy, "https": proxy}

for _ in range(3):
    print(s.get("https://api.ipify.org", timeout=30).text)  # same IP three times
```

Call `sticky_proxy()` again to get a new session number, and with it a different IP. Give each account or task its own session.

### SOCKS5

SOCKS support is an optional extra of requests:

```text
python -m pip install "requests[socks]"
```

Use the `socks5h` scheme so that hostnames are resolved by the proxy rather than by your machine. On Residential and the mobile plans, SOCKS5 uses the same host and port as HTTP:

```python
import requests

PROXY = "socks5h://USERNAME:PASSWORD@HOST:PORT"

r = requests.get(
    "https://api.ipify.org?format=json",
    proxies={"http": PROXY, "https": PROXY},
    timeout=30,
)
print(r.json())
```

### From a proxy string to a URL

The dashboard shows proxies as `host:port:username:password`. This function turns that string into the URL form and percent-encodes the credentials, which matters when a password contains `@`, `:` or `/`:

```python
from urllib.parse import quote

def to_proxy_url(proxy_string, scheme="http"):
    host, port, user, password = proxy_string.split(":", 3)
    return f"{scheme}://{quote(user, safe='')}:{quote(password, safe='')}@{host}:{port}"

print(to_proxy_url("HOST:PORT:USERNAME:PASSWORD"))
# http://USERNAME:PASSWORD@HOST:PORT
```

Keep credentials out of source control: read the proxy string from an environment variable or a local configuration file.

### Timeouts and retries

Always set a timeout; requests waits indefinitely without one. For transient failures, mount an adapter with a retry policy. A Session reuses open connections, and requests sent over one open connection share its exit IP. If every retry must leave from a new IP, send the header `Connection: close` (`s.headers["Connection"] = "close"`) or call `requests.get` without a Session:

```python
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

PROXY = "http://USERNAME:PASSWORD@HOST:PORT"

def make_session():
    retry = Retry(
        total=3,
        backoff_factor=1,
        status_forcelist=[429, 500, 502, 503, 504],
        allowed_methods=["GET", "HEAD"],
    )
    s = requests.Session()
    s.proxies = {"http": PROXY, "https": PROXY}
    s.mount("http://", HTTPAdapter(max_retries=retry))
    s.mount("https://", HTTPAdapter(max_retries=retry))
    return s

session = make_session()
try:
    r = session.get("https://api.ipify.org?format=json", timeout=(10, 30))
    r.raise_for_status()
    print(r.json())
except requests.exceptions.ProxyError as e:
    print("Proxy refused or unreachable:", e)
except requests.exceptions.Timeout:
    print("Timed out")
except requests.exceptions.RequestException as e:
    print("Request failed:", e)
```

The timeout tuple is (connect, read) in seconds. Keep the retry count low and respect `429` responses: they mean the target wants fewer requests, and slowing down works better than retrying faster. The [web scraping page](https://coreproxies.com/use-case/web-scraping/) covers which proxy type to start with.

### Errors you may see

| Exception or status | Meaning | What to check |
| --- | --- | --- |
| ProxyError with 407 | The proxy rejected the credentials | Username with its suffix, password, special characters encoded, plan still active |
| ConnectTimeout | The proxy could not be reached in time | Host and port; outbound firewall rules on your server |
| InvalidSchema: Missing dependencies for SOCKS support | The SOCKS extra is not installed | Install `requests[socks]` |
| Your own IP in the output | The request did not use the proxy | Both `http` and `https` keys are set; `NO_PROXY` does not match the target |

To isolate a problem from your code, run the same proxy through curl first: see [testing a proxy on Linux](https://coreproxies.com/help/article/how-to-test-a-proxy-on-linux/) or [on Windows](https://coreproxies.com/help/article/how-to-test-a-proxy-on-windows/).

### Run it against your own plan

Create an account, choose your proxy type and plan, and copy the proxy string from your dashboard into the examples above.

[Get started](https://coreproxies.com/register/) · [All help articles](https://coreproxies.com/help/)

Next article

[Using Core Proxies with C# and VB.NET →](https://coreproxies.com/help/article/visual-basic/)
