Socks5Proxy

Python requests proxy setup: Connecting SOCKS5 and HTTP proxies

This guide details Python requests setup with Socks5Proxy HTTP/SOCKS5, auth, list rotation, and retries.

What do I need to prepare?

Requires Python 3.8+ and requests library. For SOCKS5, install requests[socks] bundle (includes PySocks) to enable socks5/socks5h addresses.

Find proxy details in your dashboard's 'My Proxies' section. Example: IP 198.51.100.23, HTTP port 8000, SOCKS5 port 1080, username spx_user, password spx_pass. Replace with your actual credentials.

pip install "requests[socks]"

How do I connect HTTP proxy?

requests sends requests through the proxy when the address is added to the proxies dictionary. It’s important to add the same HTTP proxy address to both http and https keys. Without the https key, HTTPS requests bypass the proxy and go directly.

HTTPS requests tunnel through proxies via CONNECT, remaining encrypted throughout. This code verifies proxy IP visibility by requesting an IP check address.

import requests

proxy = "http://spx_user:spx_pass@198.51.100.23:8000"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=20)
print(r.status_code, r.json())  # the "ip" field should show the proxy IP

How do I connect SOCKS5 proxies?

We recommend using socks5h:// prefix. socks5h lets the proxy handle DNS, hiding your server's requests and resolving domains based on the proxy's country. Plain socks5 resolves domains locally before sending IPs to the proxy.

Use the SOCKS5 port. Connecting SOCKS5 addresses to HTTP ports won’t work, so distinguish between the two ports listed per line.

import requests

proxy = "socks5h://spx_user:spx_pass@198.51.100.23:1080"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=20)
print(r.json())

How do I rotate through multiple proxies for requests?

For collecting many pages, the default method is to download a list and use the next proxy for each request. The code below fetches a SOCKS5 list via API, cycles through it sequentially, and retries failed requests up to 3 times with a different proxy. It also includes a short pause to avoid sending requests too quickly to the same domain.

Instead of discarding failed proxies immediately, temporarily pause and reuse them later. Many failures stem from temporary target site issues.

import itertools
import time
import requests

API = "https://socks5proxy.io/api/v1"
KEY = "YOUR_API_KEY"

lines = requests.get(
    API + "/proxies/download",
    params={"format": "user:pass@ip:port", "protocol": "socks5"},
    headers={"Authorization": "Bearer " + KEY},
    timeout=30,
).text.split()
pool = itertools.cycle(lines)

def fetch(url, tries=3):
    for _ in range(tries):
        p = "socks5h://" + next(pool)
        try:
            r = requests.get(url, proxies={"http": p, "https": p}, timeout=20)
            if r.status_code == 200:
                return r.text
        except requests.RequestException:
            pass
        time.sleep(1)
    return None

for page in range(1, 6):
    html = fetch("https://example.com/list?page=" + str(page))
    print(page, "ok" if html else "failed")
    time.sleep(0.5)

How to maintain sessions and cookies?

For cookie-dependent workflows (logins/pagination), bind one proxy per requests.Session. Setting the proxy in session.proxy ensures all requests from that session use the same IP. Close sessions when done and create new ones for different proxies.

Consistent User-Agent and Accept-Language headers per session make your activity appear uniform to target sites.

import requests

s = requests.Session()
s.proxies = {
    "http": "http://spx_user:spx_pass@198.51.100.23:8000",
    "https": "http://spx_user:spx_pass@198.51.100.23:8000",
}
s.headers.update({"User-Agent": "Mozilla/5.0", "Accept-Language": "en-US,en;q=0.9"})

s.get("https://example.com/", timeout=20)          # cookies are stored in the session
r = s.get("https://example.com/account", timeout=20)  # same IP, same cookies
print(r.status_code)
s.close()

How do I use IP authorization?

Register the server's IPv4 running this code in the dashboard to use addresses without credentials. Avoid storing passwords in code or config files, simplifying deployment across servers. Choose ip:port format when downloading lists.

import requests

proxy = "http://198.51.100.23:8000"  # no user or password, server IP is authorized in the dashboard
r = requests.get("https://api.ipify.org", proxies={"http": proxy, "https": proxy}, timeout=20)
print(r.text)

How do I send multiple requests simultaneously and quickly?

requests waits for each request to complete, so to fetch many pages quickly, run multiple tasks concurrently. The easiest way is with concurrent.futures' ThreadPoolExecutor. Keep the number of tasks below the number of proxies to avoid overloading a single IP.

The code below divides the address list among 20 workers, and each worker selects the next proxy from the list to use. While itertools.cycle can be used across multiple threads without issues, if precise order is required, use locking.

import itertools
from concurrent.futures import ThreadPoolExecutor
import requests

lines = open("proxies.txt").read().split()  # user:pass@ip:port per line
pool = itertools.cycle(lines)

def get(url):
    p = "http://" + next(pool)
    try:
        return url, requests.get(url, proxies={"http": p, "https": p}, timeout=20).status_code
    except requests.RequestException as e:
        return url, type(e).__name__

urls = ["https://example.com/item/" + str(i) for i in range(200)]
with ThreadPoolExecutor(max_workers=20) as ex:
    for url, result in ex.map(get, urls):
        print(url, result)

Can I use async libraries like httpx or aiohttp?

Yes. Pass proxy addresses to httpx via the proxy argument. Install httpx[socks] for SOCKS5 support. AsyncClient accepts the same parameters.

aiohttp requires HTTP proxy addresses in the proxy parameter per request, with auth via aiohttp.BasicAuth. For SOCKS5, use aiohttp-socks connectors (not natively supported).

Async processing handles thousands of requests efficiently, but unlimited concurrency overwhelms targets. Use asyncio.Semaphore to limit simultaneous requests.

import httpx

with httpx.Client(proxy="http://spx_user:spx_pass@198.51.100.23:8000", timeout=20) as client:
    print(client.get("https://api.ipify.org").text)

How do I resolve frequent errors?

The 'Missing dependencies for SOCKS support' error means requests[socks] isn't installed. Please reinstall it in the same virtual environment.

407 Proxy Authentication Required occurs when the username/password is incorrect or the server IP isn't registered for IP authorization. If your password contains special characters, encode them using urllib.parse.quote.

Connection timeouts are often due to incorrect ports. Ensure you use the HTTP port for HTTP addresses and the SOCKS5 port for socks5h addresses. Including a timeout value prevents unresponsive requests from halting the entire task.

  • SOCKS error: pip install "requests[socks]"
  • 407: Check username/password or registered IP
  • Timeout: HTTP port and SOCKS5 port distinction
  • Add Timeout to All Requests
Python requests proxy setup: SOCKS5/HTTP connections | Socks5Proxy