# Python Requests Proxy: Authentication, SOCKS5, Rotation

Source: https://coreproxies.com/integrations/python-requests/

Python Requests · Integration guide

## Python Requests proxy setup

Requests reads a proxy as a URL. Put the four values from your dashboard into that URL, pass it in the `proxies` dictionary, and the username decides whether the IP rotates or stays.

Last updated: November 27, 2026

[Get started](https://coreproxies.com/register/) · [See pricing](https://coreproxies.com/pricing/)

### Connection fields

All four dashboard values fit into one proxy URL.

The dashboard shows `HOST:PORT:USERNAME:PASSWORD`. Requests wants the same data in URL order: `http://USERNAME:PASSWORD@HOST:PORT`. Build that string once and use it for both keys of the `proxies` dictionary:

- **"http"**: the proxy used for `http://` targets.
- **"https"**: the proxy used for `https://` targets. Its value still starts with `http://`, because that scheme describes how you talk to the proxy, not to the site.

If the username or password contains characters such as `@`, `:` or `/`, pass each through `urllib.parse.quote(value, safe="")` before building the URL. Requests also honours the `HTTP_PROXY` and `HTTPS_PROXY` environment variables, which is handy for scripts you cannot edit; an explicit `proxies` argument takes priority over them.

| Product | Host and port | Protocols | Authentication |
| --- | --- | --- | --- |
| Residential and Unlimited Residential | shown in your dashboard | HTTP, HTTPS and SOCKS5 on the same port | username:password |
| Rotating Mobile | shown in your dashboard | HTTP(S) and SOCKS5 on the same port | username:password |
| Mobile Proxy (Reset Link) | shown in your dashboard | HTTP(S) and SOCKS5 on the same port | username:password |
| Datacenter, ISP and IPv6 | shown with your order in the dashboard | HTTP(S) and SOCKS5 on separate ports | username:password |

HOST, PORT, USERNAME and PASSWORD in the examples are placeholders. Your dashboard shows the host, port, username and password for each plan: copy them from the Proxy generator.

### HTTP(S) or SOCKS5

Requests speaks both protocols; SOCKS5 needs one extra package.

| Protocol | Residential and mobile plans | Datacenter, ISP and IPv6 | In Requests |
| --- | --- | --- | --- |
| HTTP(S) | host and port from your dashboard | HTTP(S) port shown with your order | Built in. URL scheme http:// |
| SOCKS5 | same host and port | SOCKS5 port shown with your order | Needs requests\[socks\]. URL scheme socks5h:// |

HTTP(S) needs nothing beyond Requests itself and is the simplest choice. For SOCKS5, install the extra with `pip install "requests[socks]"` and use the `socks5h://` scheme. The trailing `h` makes the proxy resolve the target hostname; plain `socks5://` resolves it on your machine first, which sends DNS lookups outside the proxy. On Residential and mobile plans SOCKS5 uses the same host and port as HTTP(S); Datacenter, ISP and IPv6 orders show a separate SOCKS5 port.

```python
# pip install "requests[socks]"
import requests

# Copy HOST, PORT, USERNAME and PASSWORD from your dashboard (Proxy generator).
# socks5h: the proxy resolves the target hostname (socks5 would resolve it locally)
PROXY = "socks5h://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())
```

### Authentication example

A complete request through the proxy with a username and password.

```python
import requests

# Copy HOST, PORT, USERNAME and PASSWORD from your dashboard (Proxy generator).
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 the proxy
```

Replace `HOST`, `PORT`, `USERNAME` and `PASSWORD` with the values from your dashboard; copy them from the Proxy generator. Always set `timeout`: without it a stalled connection blocks the script indefinitely.

The same code works for every plan, from [Datacenter](https://coreproxies.com/product/datacenter/) and [ISP](https://coreproxies.com/product/isp/) to [Residential](https://coreproxies.com/product/residential/) and mobile plans; only the four values change.

### Rotation and sticky sessions

Change the username, not the code, to switch between a new IP per connection and one IP for a whole task.

The script below does both on a Residential plan. The first loop uses the plain username; each `requests.get()` call opens a new connection, so it normally prints a different address on each pass (an address can repeat now and then). The second loop builds a sticky username with a random six-digit session number and prints the same address three times.

```python
import random

import requests

# Residential plan: copy these four values from your dashboard (Proxy generator).
HOST = "HOST"
PORT = "PORT"
USERNAME = "USERNAME"
PASSWORD = "PASSWORD"
URL = "https://api.ipify.org?format=json"

def proxies_for(username):
    proxy = f"http://{username}:{PASSWORD}@{HOST}:{PORT}"
    return {"http": proxy, "https": proxy}

# Rotating: no session in the username. Each call opens a new connection,
# so it normally gets a new IP (an address can repeat now and then).
for _ in range(3):
    print("rotating:", requests.get(URL, proxies=proxies_for(USERNAME), timeout=30).json())

# Sticky: a session number (digits only) keeps the same IP for the ttl (600 seconds).
session_id = random.randint(100000, 999999)
sticky = proxies_for(f"{USERNAME}-session-{session_id}-ttl-600")
for _ in range(3):
    print("sticky:", requests.get(URL, proxies=sticky, timeout=30).json())
```

Use a separate session number for each task that needs its own IP, and reuse the number for as long as that task should stay on one address. A `requests.Session` object is a different thing: it keeps cookies and reuses open connections, which is useful with a sticky username because the whole login flow then shares cookies and an IP. With the plain rotating username, a `Session` reuses its open connection, so repeated requests to the same site can keep the same IP; use separate `requests.get()` calls when each request should get a new one.

| Product | New IP | Same IP | Location |
| --- | --- | --- | --- |
| Residential and Unlimited Residential | `USERNAME` (every new connection) | `USERNAME-session-482913-ttl-600` | `USERNAME-country-de` |
| Rotating Mobile | `USERNAME` (every new connection) | `USERNAME-session-482913-ttl-600` | chosen in your dashboard |
| Mobile Proxy (Reset Link) | Change IP button or reset link | `USERNAME` keeps one IP for up to 24 hours | chosen in your dashboard |
| Datacenter, ISP and IPv6 | set in the Proxy generator | set in the Proxy generator | set in the Proxy generator |

On Residential and Unlimited Residential the username on its own rotates, so every new connection gets a new IP. For a sticky session add `-session-` with a number and `-ttl-` with the time in seconds. Sticky sessions last up to 24 hours; the dashboard offers 1 minute, 10 minutes, 30 minutes, 1 hour, 6 hours and 24 hours, or a custom time. Use a different session number for each account or task; a new number gives you a new IP. On Datacenter, ISP and IPv6, the Proxy generator of your order builds the username for you: copy the generated lines from there. Details are in the [rotating proxies guide](https://coreproxies.com/rotating-proxies/).

For IPs from one country on Residential or Unlimited Residential, add `-country-` and a two-letter country code to the username, for example `USERNAME-country-de`. State and city are chosen in the Proxy generator, which adds them to the username for you. Location and session combine as `USERNAME-country-de-session-482913-ttl-600`:

```python
from urllib.parse import quote

import requests

# Residential and Unlimited Residential: copy these four values from your dashboard (Proxy generator).
HOST = "HOST"
PORT = "PORT"
USERNAME = "USERNAME"
PASSWORD = "PASSWORD"

# IPs from Germany, one IP held for 600 seconds.
# Use f"{USERNAME}-country-de" on its own to rotate inside the country.
username = f"{USERNAME}-country-de-session-482913-ttl-600"

# quote() keeps characters such as @ : / in a password from breaking the proxy URL.
proxy = f"http://{quote(username, safe='')}:{quote(PASSWORD, safe='')}@{HOST}:{PORT}"

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

The example also shows `quote()` around the username and password, which keeps characters such as `@` or `:` in a password from breaking the proxy URL.

On Rotating Mobile you choose All countries or a single country in your dashboard, and the carrier inside that country is picked automatically. The username carries only the session: without one, every new connection gets a new carrier IP.

On Rotating Mobile the session ID must be numbers only (digits 0–9); an ID with letters is ignored and the IP keeps rotating. The number is only an example: pick your own, such as a random 6-digit number, and use a different one for each account or task. The ttl value is how long you want to keep the IP, in seconds. Mobile networks can still change an IP on their own, so plan for an occasional change.

Mobile Proxy (Reset Link) keeps one carrier IP for up to 24 hours. Change it with the Change IP button or your reset link, once every 30 seconds; changing the location in your dashboard also gives you a new IP.

### Test the connection

One request tells you whether the proxy is in the path.

`https://api.ipify.org?format=json` returns the address your request arrived from. Run it once without `proxies` and once with, and the two results should differ. To check your own address in a browser, open the [My IP tool](https://coreproxies.com/tools/my-ip/).

The script below separates the failure cases, which makes the next section easier to apply:

```python
import requests

# Copy HOST, PORT, USERNAME and PASSWORD from your dashboard (Proxy generator).
PROXY = "http://USERNAME:PASSWORD@HOST:PORT"
proxies = {"http": PROXY, "https": PROXY}

try:
    # (connect timeout, read timeout) in seconds
    r = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=(10, 30))
    r.raise_for_status()
    print("exit IP:", r.json()["ip"])
except requests.exceptions.ProxyError as error:
    if "407" in str(error):
        print("the proxy rejected the login: check the username and password")
    else:
        print("could not reach the proxy, or it refused the tunnel: check host, port and firewall")
except requests.exceptions.ConnectTimeout:
    # Older urllib3 releases report an unreachable proxy this way.
    print("could not reach the proxy: check host, port and firewall")
except requests.exceptions.ReadTimeout:
    print("connected, but the target did not answer in time")
except requests.exceptions.RequestException as error:
    print("request failed:", error)
```

### Troubleshooting

Requests raises a different exception for each kind of failure.

#### 407 Proxy Authentication Required

For an HTTPS target this arrives as `requests.exceptions.ProxyError` with "407 Proxy Authentication Required" in the message; for a plain HTTP target the response itself has status 407. Check the username and its suffix and the password, encode special characters with `quote()`, and make sure the credentials belong to the plan whose host you are using. An unexpected proxy from `HTTPS_PROXY` in the environment can cause the same error; set `session.trust_env = False` to rule it out.

#### Timeouts and connection errors

A `ProxyError` that says "Unable to connect to proxy" without a 407 means the proxy was not reached or refused the tunnel: check the host, the port and outbound firewall rules for the proxy port. Older urllib3 releases report an unreachable proxy as `ConnectTimeout` instead, which is why the test script catches both. `ReadTimeout` means the connection was made but the target was slow to answer; raise the second value of the `timeout` tuple or retry. An error that mentions missing SOCKS dependencies means `requests[socks]` is not installed.

#### Target ports by plan

On Residential and Unlimited Residential plans, residential IPs connect to websites on ports 80 and 443 only: a URL that names another port, such as `:8443`, fails even when the proxy login is correct.

All target ports are open on Rotating Mobile and Mobile Proxy (Reset Link) plans.

### Python Requests proxy setup: frequently asked questions

#### Why does the https key use an http:// proxy URL?

The key is the scheme of the target; the value is how Requests connects to the proxy. HTTPS traffic is tunnelled through the HTTP(S) port with CONNECT, so the value starts with http://.

#### How do I use a SOCKS5 proxy with Python Requests?

Install requests\[socks\] and use a socks5h:// proxy URL with the host and port from your dashboard. On Residential and mobile plans that is the same port as HTTP(S). The h makes the proxy resolve hostnames.

#### How do I get a new IP for every request?

On Residential and Unlimited Residential, use the username on its own: every new connection gets a new IP, so make separate requests.get() calls rather than one Session. On Datacenter, ISP and IPv6, copy the rotating lines from the Proxy generator.

#### How do I keep the same IP across several requests?

Use a sticky username such as USERNAME-session-482913-ttl-600 on Residential. Pick your own session number (digits only) and set the ttl in seconds; sticky sessions last up to 24 hours.

#### My password has special characters. What should I do?

Encode the username and password with urllib.parse.quote(value, safe="") before placing them in the proxy URL.

[All frequently asked questions](https://coreproxies.com/faq/)

Get started

### Send Python requests through Core Proxies

Create an account, choose your proxy type and plan, and copy the host, port, username and password from your dashboard. Activation is automatic after payment.

- Rotating and sticky sessions
- HTTP(S) and SOCKS5
- 24/7 support by email

[Get started](https://coreproxies.com/register/) · [Talk to sales](mailto:info@coreproxies.com)

**Plans to start with** · Datacenter and ISP

**Datacenter** · from €15 / month · 250 GB · 100 IPs · [See pricing](https://coreproxies.com/datacenter/)

**ISP** · from €18.23 / 250 GB + €0.23 / IP · [See pricing](https://coreproxies.com/isp/)

Prices exclude VAT

### Related pages
