İçeriğe geç
Guides 3 dk okuma 611 görüntülenme

Using Proxies in Python with httpx

Working with proxies via httpx: synchronous and asynchronous modes, SOCKS5, HTTP/2 via proxy, and comparison with requests.

Using Proxies in Python with httpx

Using Proxies in Python with httpx

httpx and Proxies

httpx is a modern HTTP library for Python that supports both synchronous and asynchronous modes of operation. A key advantage is its built-in support for HTTP/2 and SOCKS proxies without additional libraries.

Installation

pip install httpx
# For SOCKS support:
pip install httpx[socks]
# For HTTP/2:
pip install httpx[http2]

Synchronous Mode

HTTP Proxy

import httpx

proxy = "http://user:pass@proxy_ip:port"
response = httpx.get("https://httpbin.org/ip", proxy=proxy)
print(response.json())

Different Proxies for HTTP and HTTPS

proxies = {
    "http://": "http://proxy1:port",
    "https://": "http://proxy2:port",
}

with httpx.Client(proxy=proxies) as client:
    response = client.get("https://httpbin.org/ip")
    print(response.json())

SOCKS5 Proxy

import httpx

proxy = "socks5://user:pass@proxy_ip:port"
with httpx.Client(proxy=proxy) as client:
    response = client.get("https://httpbin.org/ip")
    print(response.json())

Asynchronous Mode

Basic Example

import httpx
import asyncio

async def fetch():
    proxy = "http://user:pass@proxy_ip:port"
    async with httpx.AsyncClient(proxy=proxy) as client:
        response = await client.get("https://httpbin.org/ip")
        print(response.json())

asyncio.run(fetch())

Parallel Requests

import httpx
import asyncio

async def fetch_url(client, url):
    try:
        response = await client.get(url, timeout=10)
        return response.text
    except Exception as e:
        return None

async def main():
    proxy = "http://user:pass@proxy_ip:port"
    urls = [f"https://example.com/page/{i}" for i in range(50)]

    async with httpx.AsyncClient(proxy=proxy) as client:
        tasks = [fetch_url(client, url) for url in urls]
        results = await asyncio.gather(*tasks)
        success = sum(1 for r in results if r)
        print(f"Success: {success}/{len(urls)}")

asyncio.run(main())

HTTP/2 via Proxy

httpx is one of the few Python libraries with HTTP/2 support:

import httpx

proxy = "http://user:pass@proxy_ip:port"
with httpx.Client(proxy=proxy, http2=True) as client:
    response = client.get("https://httpbin.org/ip")
    print(f"HTTP version: {response.http_version}")
    print(response.json())

HTTP/2 via proxy works through the CONNECT method — the proxy creates a TCP tunnel through which an HTTP/2 connection to the server is established.

Proxy Rotation

import httpx
import random

PROXIES = [
    "http://user:pass@proxy1:port",
    "http://user:pass@proxy2:port",
    "http://user:pass@proxy3:port",
]

def get_random_proxy():
    return random.choice(PROXIES)

# Synchronous rotation
for i in range(10):
    proxy = get_random_proxy()
    with httpx.Client(proxy=proxy) as client:
        resp = client.get("https://httpbin.org/ip")
        print(resp.json())

Asynchronous Rotation

import httpx
import asyncio
import random

PROXIES = [
    "http://user:pass@proxy1:port",
    "http://user:pass@proxy2:port",
    "http://user:pass@proxy3:port",
]

async def fetch_with_rotation(url):
    proxy = random.choice(PROXIES)
    async with httpx.AsyncClient(proxy=proxy) as client:
        try:
            resp = await client.get(url, timeout=10)
            return resp.text
        except Exception:
            return None

async def main():
    urls = ["https://example.com"] * 20
    tasks = [fetch_with_rotation(url) for url in urls]
    results = await asyncio.gather(*tasks)
    print(f"Success: {sum(1 for r in results if r)}")

asyncio.run(main())

Client Configuration

Timeout

timeout = httpx.Timeout(
    connect=5.0,    # connection to proxy and server
    read=10.0,      # reading the response
    write=5.0,      # sending the request
    pool=5.0        # waiting in the connection pool
)

client = httpx.Client(proxy=proxy, timeout=timeout)

Headers and Cookies

headers = {
    "User-Agent": "Mozilla/5.0 ...",
    "Accept-Language": "en-US,en;q=0.9",
}

client = httpx.Client(
    proxy=proxy,
    headers=headers,
    follow_redirects=True,
    max_redirects=10
)

Connection Pool

limits = httpx.Limits(
    max_connections=100,          # total connections
    max_keepalive_connections=20  # keep-alive connections
)

client = httpx.AsyncClient(proxy=proxy, limits=limits)

Error Handling

import httpx

async def safe_fetch(client, url):
    try:
        response = await client.get(url)
        response.raise_for_status()
        return response.text
    except httpx.ProxyError as e:
        print(f"Proxy error: {e}")
    except httpx.ConnectTimeout:
        print("Connection timeout")
    except httpx.ReadTimeout:
        print("Read timeout")
    except httpx.HTTPStatusError as e:
        print(f"HTTP {e.response.status_code}")
    except httpx.RequestError as e:
        print(f"Request error: {e}")
    return None

httpx vs requests vs aiohttp

Parameter httpx requests aiohttp
Sync Yes Yes No
Async Yes No Yes
HTTP/2 Yes No No
SOCKS Yes (built-in) Via requests-socks Via aiohttp-socks
Proxy in URL Yes Yes Yes
API requests-compatible Standard Custom
Performance High Medium High

Migration from requests

httpx is designed as a drop-in replacement for requests. Migration is minimal:

# requests
import requests
resp = requests.get(url, proxies={"https": proxy})

# httpx
import httpx
resp = httpx.get(url, proxy=proxy)

The main difference: in httpx, the proxy parameter (singular) is used instead of proxies.

Conclusion

httpx is the best choice for new Python projects requiring proxy functionality. Its built-in support for HTTP/2, SOCKS5, and both synchronous and asynchronous modes makes it a versatile tool. The requests-compatible API simplifies migration for existing projects.

Güncellendi: 06.03.2026
Kategoriye dön

Bunları da okuyun

Guides 2 dk

Nike SNKRS için Proxy: Sınırlı Sürümler ve Çoklu Katılım

Nike SNKRS'ta birden fazla hesapla çekilişlere katılmak, IP limitlerini aşmak ve ban yemeden sınırlı sürümleri kapmak için residential veya mobil proxy kullanın. Hangi türü seçmeli ve nasıl kurmalı.

Guides 2 dk

iPhone'da Proxy Nasıl Kurulur (Wi-Fi ve SOCKS5)

iPhone'da proxy'yi yerleşik Wi-Fi ayarlarından (HTTP/HTTPS) veya SOCKS5 ile hücresel kapsama için Shadowrocket gibi bir uygulamayla kurun. Adım adım iOS kurulumu ve bilinmesi gereken kısıtlar.

Guides 3 dk

Tinder için proxy: çoklu hesap ve ban'lerden kaçınma

Birden fazla profil işletmek ve IP shadowban'lerinden kaçınmak için Tinder'da mobil veya residential proxy kullanın. Mobilin neden kazandığı, kurulumu ve proxy'nin şehri neden değiştirmediği.

Guides 2 dk

StockX için proxy: fiyat takibi ve hesap yönetimi

StockX'te fiyatları izlemek, birden fazla hesap çalıştırmak ve drop'ları yasaklanmadan kapmak için residential, ISP veya mobil proxy kullanın. Hangi türü seçeceğinizi ve nasıl kuracağınızı anlatıyoruz.

Guides 3 dk

OpenAI API için Proxy: Erişim, Hız Limitleri ve Kurulum

OpenAI API'yi residential veya ISP proxy üzerinden yönlendirerek desteklenen bölgelere erişin, 429 hız limitlerinden kaçının ve hesapları izole edin. Hangi proxy'yi seçmeli ve Python kurulum örnekleri.

Guides 1 dk

E2E testleri için Cypress'te proxy kurulumu

Cypress'te proxy kurulumu: HTTP_PROXY değişkenleri, cy-proxy-middleware ve coğrafi konuma bağlı içeriğin test edilmesi.

Proxy'lerimizi deneyin

100+ ülkede 20,000+ proxy

support_agent
GProxy Support
Usually replies within minutes
Hi there!
Send us a message and we'll reply as soon as possible.