Utiliser des proxys en Python et Node.js
Exemples de code pour requests, httpx, aiohttp, Node.js fetch et axios — avec HTTP et SOCKS5, authentification et rotation d’IP.
Pour un scraper, un bot ou une automatisation, ajouter un proxy prend quelques lignes. Les données au format host:port:login:password sont dans « Mes proxys ».
Python : requests
import requests
proxy = "http://login:password@host:port"
r = requests.get("https://ipinfo.io/json", proxies={"http": proxy, "https": proxy}, timeout=30)
print(r.json()["ip"])
Pour SOCKS5 : pip install requests[socks] et le schéma socks5h://.
Python : httpx et aiohttp
import httpx
async with httpx.AsyncClient(proxy="http://login:password@host:port", timeout=30) as client:
r = await client.get("https://ipinfo.io/json")
import aiohttp
async with aiohttp.ClientSession() as s:
async with s.get("https://ipinfo.io/json", proxy="http://login:password@host:port") as r:
print(await r.json())
Node.js : undici / fetch
import { ProxyAgent, fetch } from 'undici';
const agent = new ProxyAgent('http://login:password@host:port');
const res = await fetch('https://ipinfo.io/json', { dispatcher: agent });
Node.js : axios
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
const agent = new HttpsProxyAgent('http://login:password@host:port');
const { data } = await axios.get('https://ipinfo.io/json', { httpsAgent: agent, proxy: false });
Pour SOCKS5 en Node.js : le paquet socks-proxy-agent.
Rotation d’IP
Proxys privés : choisissez au hasard ou à tour de rôle dans la liste. Résidentiels et mobiles : l’IP change toute seule ; pour la garder, login-session-ab12cd-time-10 (détails).
Achetez et renouvelez des proxys depuis le code avec l’API ProxyZen.
Erreurs fréquentes
- 407 — identifiants erronés.
- Timeout — mauvais port ; résidentiels et mobiles : HTTP 8080, SOCKS5 1080.
- Erreurs SSL — généralement un mauvais schéma (
http://pour un proxy HTTP, même pour un site HTTPS).
Essayez ProxyZen
IPv4 et IPv6 dès $0.05, résidentiels et mobiles au Go. Livraison immédiate, HTTP(S) et SOCKS5.