BlessProxy

How to Use SOCKS5 Proxies in Python (requests)

Route Python requests through a SOCKS5 proxy with login and password: installation, socks5 vs socks5h, sessions, timeouts and error handling.

· 2 min read

The requests library supports SOCKS5 proxies through the optional PySocks dependency. This guide shows a working setup with username/password authentication. The examples were run with Python 3.14 and requests 2.34.

Install requests with SOCKS support

pip install "requests[socks]"

Without the socks extra, requests raises InvalidSchema: Missing dependencies for SOCKS support.

Make a request through the proxy

import requests

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

r = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=30)
print(r.json())  # {'ip': '<the proxy exit IP>'}

The proxies dictionary maps the target URL scheme to a proxy. Set both http and https, otherwise HTTPS requests bypass the proxy.

socks5 vs socks5h

  • socks5:// — your computer resolves the domain name and sends the IP address to the proxy.
  • socks5h:// — the domain name is sent to the proxy and resolved there.

Use socks5h:// in most cases: DNS queries do not leak from your machine, and the target is resolved from the proxy location.

Reuse a session

import requests

session = requests.Session()
session.proxies.update({"http": PROXY, "https": PROXY})

for url in ["https://api.ipify.org", "https://api.ipify.org?format=json"]:
    print(session.get(url, timeout=30).text)

A session keeps connections and cookies between requests.

Handle errors and timeouts

Always set timeout. A wrong password, an unreachable proxy or a closed port raise exceptions derived from requests.exceptions.RequestException (a rejected SOCKS login usually shows up as ConnectionError):

try:
    r = requests.get("https://api.ipify.org", proxies=proxies, timeout=15)
    r.raise_for_status()
except requests.exceptions.RequestException as exc:
    print("Proxy request failed:", type(exc).__name__, exc)

Special characters in the password

If the login or password contains @, : or /, URL-encode them:

from urllib.parse import quote

proxy = f"socks5h://{quote(login, safe='')}:{quote(password, safe='')}@{host}:{port}"

Where to get the connection details

On BlessProxy every proxy shows a ready socks5://login:password@host:port string in “My proxies”. Mobile 4G/5G ports also accept HTTP, while static residential and datacenter IPs work over SOCKS5 only — see SOCKS5 proxies.