Mobile Proxy Python Setup
Step-by-step mobile proxy Python setup tutorial. Configure requests, httpx, aiohttp, and Selenium for resilient automation and web scraping with real 4G/5G carrier IPs.
- requests Session pooling — maintain sticky sessions and reuse TCP connections across hundreds of HTTP requests.
- High-concurrency httpx & aiohttp — configure async client transports with full HTTP/2 and SOCKS5 proxy support.
- Headless Selenium integration — inject proxy arguments and handle browser authentication extensions seamlessly.
- Resilient retry pipelines — automatically detect IP rotation cooldowns and retry transient carrier drops.
Python Proxy Libraries
Python is the primary language for web scraping, automation, and data pipeline engineering. However, different Python libraries interact with proxies at different layers of the networking stack. Choosing the right library depends on whether your project requires simple synchronous HTTP calls, high-concurrency asynchronous workflows, or full browser emulation.
| Library | Paradigm | SOCKS5 Support | Best For |
|---|---|---|---|
| requests | Synchronous | Via requests[socks] | Scripts, linear scrapers, API clients |
| httpx | Sync & Async (HTTP/2) | Native via socksio | High-throughput scrapers, FastAPI backends |
| aiohttp | Async IO | Via aiohttp-socks | Massive concurrent web scrapers |
| Selenium | Browser WebDriver | Native flags | Single-page apps, Cloudflare Turnstile, login flows |
requests Library Setup
In Python requests, proxies are configured using a dictionary that maps protocol schemes to the proxy endpoint URL. To avoid overhead and TCP renegotiation on every call, always use a requests.Session() object. A session reuses the underlying connection pool and persists cookies across calls.
import requests
# 1. Define proxy credentials and endpoint
PROXIES = {
'http': 'http://user123:secret456@proxy.pxm2.io:8000',
'https': 'http://user123:secret456@proxy.pxm2.io:8000',
}
# 2. Initialize Session and mount proxies
session = requests.Session()
session.proxies.update(PROXIES)
# 3. Add realistic mobile browser headers
session.headers.update({
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15',
'Accept-Language': 'en-US,en;q=0.9',
})
# 4. Make requests through the dedicated mobile IP
response = session.get('https://ipinfo.io/json', timeout=15)
print(response.json())
To route requests through SOCKS5 with remote DNS resolution, install the SOCKS dependencies: pip install "requests[socks]". Then replace the proxy URLs with socks5h://user:pass@proxy.pxm2.io:1080.
httpx and aiohttp Configuration
When scaling scrapers to hundreds of requests per minute, synchronous code blocks the Python interpreter while waiting for network responses. httpx and aiohttp allow concurrent requests on a single async event loop.
Using httpx with Async Mobile Proxies
httpx provides modern async support and clean proxy configuration directly on the AsyncClient:
import asyncio
import httpx
async def fetch_ip():
proxy_url = "http://user123:secret456@proxy.pxm2.io:8000"
# Configure AsyncClient with custom timeouts and proxy
limits = httpx.Limits(max_keepalive_connections=20, max_connections=50)
async with httpx.AsyncClient(proxy=proxy_url, limits=limits, timeout=20.0) as client:
response = await client.get("https://api.ipify.org?format=json")
data = response.json()
print(f"Carrier IP: {data['ip']}")
asyncio.run(fetch_ip())
Selenium Proxy Setup
For dynamic sites protected by advanced bot management or heavy JavaScript, Selenium drives a real Chrome or Chromium browser. However, Chrome's command-line --proxy-server flag does not support embedded credentials (e.g. user:pass@host). If credentials are passed on the flag, Chrome prompts for authentication with a GUI popup that stalls headless scripts.
There are two production solutions for Selenium authentication: IP whitelisting your server on PXM2, or injecting a lightweight background Chrome extension.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
# Point to your dedicated PXM2 mobile modem (secured by IP whitelist)
options.add_argument('--proxy-server=http://proxy.pxm2.io:8000')
options.add_argument('--headless=new')
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options)
driver.get('https://ipinfo.io')
print(driver.title)
driver.quit()
Python Proxy Best Practices
To ensure stability when scraping or managing accounts over mobile cellular connections, follow these essential engineering rules:
-
1. Implement Exponential Backoff with urllib3 RetriesCellular networks occasionally drop packets during radio handovers. Mounting an HTTPAdapter with a Retry configuration for 429, 502, 503, and 504 errors prevents scrapers from crashing.
-
2. Set Explicit TimeoutsNever make unparameterized requests in Python. If a cell tower latency spike occurs, an untimed request hangs indefinitely. Always provide a tuple: timeout=(5.0, 25.0) for connect and read timeouts.
-
3. Synchronize IP RotationsWhen calling the PXM2 rotation webhook from Python, pause active worker threads for 5 to 8 seconds. This gives the modem time to disconnect from the 4G/5G tower, renegotiate a new carrier IP, and re-establish data service.
France
India
Poland
Frequently Asked Questions
How do I configure a mobile proxy in Python requests?
Define a proxies dictionary mapping http and https protocols to your proxy URL, then pass it to requests.get() or mount it on a requests.Session() instance. Using a Session reuses underlying TCP connections and preserves cookies across calls.
Does Python requests support SOCKS5 mobile proxies?
Yes, when installed with SOCKS support via pip install requests[socks]. You can then provide a socks5h:// URL scheme to ensure hostname DNS resolution occurs on the proxy modem rather than your local machine.
How do I handle proxy authentication in Selenium without alert popups?
Chrome does not natively support inline username and password flags in --proxy-server. The standard solution is using a background Chrome extension that handles the chrome.webRequest.onAuthRequired event, or using IP whitelisting on PXM2 modems.
What is the best way to handle IP rotation during Python scraping?
Use urllib3 retry adapters mounted on your Session configured to retry on HTTP 429 and 503 status codes. When rotating on demand, send an HTTP call to your PXM2 rotation endpoint, pause for 5 to 10 seconds, and resume scraping with the new carrier IP.
How does httpx compare to requests for mobile proxy automation?
httpx provides modern async/await syntax out of the box via httpx.AsyncClient, native HTTP/2 support, and strict connection timeouts. It is ideal for high-throughput scraping tasks where asynchronous event loops process multiple targets concurrently.
Related Mobile Proxy Guides
Check out complementary tutorials for other languages, environments, and core networking architectures.
Developer & Automation Guides
Platform & Scraping Guides
Supercharge Your Python Pipelines with Dedicated Mobile IPs
Connect Python requests, httpx, and Selenium directly to real 4G/5G carrier hardware. Unlimited rotations and zero bandwidth metering.
Get a Mobile Proxy