Proxy for Python
Python is the #1 language for data parsing and automation. However, any script sooner or later hits an IP ban. To avoid this, you need to properly integrate proxies.
In this guide, we will look at how to connect a proxy in the four most popular scenarios: from simple requests to browser management.
📌 Method 1. The requests Library (Most Popular)
If you are writing a simple parser or working with an API, you use requests. Here, proxies are passed through a standard dictionary (dict).
Code:
import requests
# Format: login:password@ip:port
# In CyberYozh App, we use port 5959 for HTTP
proxy = "http://user:pass@51.77.190.247:5959"
proxies = {
"http": proxy,
"https": proxy
}
try:
# Always specify a timeout!
response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)
print(f"Your IP: {response.json()['origin']}")
except Exception as e:
print(f"Error: {e}")
Tip: Don't hardcode proxies in your code. It's better to move them to environment variables (os.getenv).
If you want to use SOCKS5 in Python, you need to:
1. Install an additional library: requests itself does not support SOCKS. You need to install an add-on:
pip install requests[socks]
# or
pip install pysocks
2. Change the scheme in the code: Here the port and syntax change to 9595 and socks5h://.
import requests
# Use socks5h to protect against DNS leaks (Remote DNS resolution)
socks_proxy = "socks5h://user:pass@51.77.190.247:9595"
proxies = {
"http": socks_proxy,
"https": socks_proxy
}
try:
response = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)
print(f"Your IP via SOCKS5: {response.json()['origin']}")
except Exception as e:
print(f"Error: {e}")
🤖 Method 2. The Selenium Library (Browser Imitation)
If a site uses complex JavaScript (React, Vue), Single Page Applications (SPA), or Cloudflare protection, regular requests won't cut it — it simply cannot execute JS code. You need Selenium, which opens and controls a real browser (Chrome/Firefox).
⚠️ The Main Problem: Authentication
Standard Chrome has an annoying peculiarity: it does not support passing proxy login and password via startup arguments. If you try to pass http://user:pass@ip:port, the browser will simply ignore the authentication and show a popup for password entry, which breaks the automation.
✅ The Solution: The selenium-wire Library
This is an extension for regular Selenium that can intercept requests and automatically inject the proxy login and password.
Installation:
pip install selenium-wire
Code (Correct setup with authentication):
# Note: import webdriver from seleniumwire, not from selenium
from seleniumwire import webdriver
# Your proxy data from CyberYozh App
PROXY_HOST = "51.77.190.247"
PROXY_PORT = "5959" # Port for HTTP
PROXY_USER = "your_login"
PROXY_PASS = "your_password"
# Form the settings dictionary
proxy_options = {
'proxy': {
'http': f'http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
'https': f'http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
'no_proxy': 'localhost,127.0.0.1' # Do not proxy local addresses
}
}
# Initialize the browser with the seleniumwire_options option
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
print("Browser started, checking IP...")
driver.get("https://httpbin.org/ip")
# Print the page content (it should contain your proxy IP)
print(driver.find_element("tag name", "body").text)
driver.quit()
Why it works: selenium-wire creates a local intermediate server that handles communication with your proxy. The browser thinks it is working without a password, while the library itself "sticks" the necessary authorization headers. This is the most reliable way for Python.
Selenium and SOCKS5:
Just as with requests, to make selenium-wire work with SOCKS proxies, you need to install the pysocks library (if it's not already installed):
pip install pysocks
Code for SOCKS5 (Selenium Wire)
The logic remains the same: the dictionary keys ('http', 'https') tell the library which traffic to intercept, and the values (socks5://...) tell it where to direct it.
from seleniumwire import webdriver # Import specifically from seleniumwire!
# Your SOCKS5 proxy data
PROXY_HOST = "51.77.190.247"
PROXY_PORT = "9595" # Ensure this is the port specifically for SOCKS5 (often different from HTTP)
PROXY_USER = "your_login"
PROXY_PASS = "your_password"
# Settings
proxy_options = {
'proxy': {
# Scheme changes to socks5://
'http': f'socks5://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
'https': f'socks5://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}',
'no_proxy': 'localhost,127.0.0.1'
}
}
# Launch browser
# You can add regular Chrome options (headless, etc.) via chrome_options=...
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
try:
print("Browser started, checking IP...")
driver.get("https://httpbin.org/ip")
# Print IP
print(driver.find_element("tag name", "body").text)
# I recommend checking the proxy type on a more detailed checker
# driver.get("https://browserleaks.com/ip")
except Exception as e:
print(f"Error: {e}")
finally:
driver.quit()
What is the difference?
In the proxy_options dictionary, we changed the scheme from http:// to socks5://.
Pay attention to the dictionary keys: We DO NOT change the keys 'http' and 'https' to 'socks'.
-
The key
'https'means: "When the browser requests HTTPS sites..." -
The value
socks5://...means: "...send them through this SOCKS5 tunnel."
What about DNS (socks5h)?
In selenium-wire, DNS resolution is handled by the library's internal logic. Usually, it handles redirecting DNS requests through the proxy automatically if the socks5 scheme is used, as selenium-wire acts as a Man-in-the-Middle (proxy) between the browser and the internet.
If you happen to notice a DNS leak (your provider is visible on checkers), try specifying socks5h:// in the connection string — selenium-wire supports this format thanks to the requests library under the hood.
🚀 Method 3. The aiohttp Library (Asynchronicity)
When you need to parse 10,000 pages per minute, an asynchronous approach (async/await) is used. Here, standard requests won't work; you need aiohttp.
Code:
import aiohttp
import asyncio
async def fetch_ip():
proxy_auth = aiohttp.BasicAuth('user', 'pass') # Login and password separately
proxy_url = "http://51.77.190.247:5959"
async with aiohttp.ClientSession() as session:
async with session.get("https://httpbin.org/ip",
proxy=proxy_url,
proxy_auth=proxy_auth) as resp:
print(await resp.json())
asyncio.run(fetch_ip())
SOCKS5 for aiohttp
For aiohttp, the situation is slightly more complex than for requests. The aiohttp library itself does not support SOCKS proxies "out of the box". It only works with HTTP proxies.
To "teach" it to work with SOCKS5, you need to use a special connector.
1. Installation
You will need an additional bridge library, aiohttp-socks:
pip install aiohttp-socks
2. Code (Correct approach via Connector)
Unlike the HTTP example where we passed the proxy into each request (session.get(..., proxy=...)), for SOCKS5 we configure the proxy once when creating the session.
This is actually better for performance (Keep-Alive connection).
import aiohttp
import asyncio
from aiohttp_socks import ProxyConnector # Import the connector
async def fetch_ip():
# Form the connection string.
# Note: embed login and password directly into the URL string.
socks_url = "socks5://user:pass@51.77.190.247:9595"
# Create the connector
# rdns=True — this is the equivalent of socks5h (DNS leak protection).
# This is MANDATORY for anonymity.
connector = ProxyConnector.from_url(socks_url, rdns=True)
# Pass the connector to ClientSession
# Now this entire session will work through SOCKS5
async with aiohttp.ClientSession(connector=connector) as session:
try:
# ATTENTION: Inside .get(), you NO LONGER need to write proxy=...
# The proxy is already "built-in" to the session via the connector.
async with session.get("https://httpbin.org/ip") as resp:
print(await resp.json())
except Exception as e:
print(f"Error: {e}")
if __name__ == '__main__':
# Correct launch for Windows/Linux
asyncio.run(fetch_ip())
🔍 What are the main differences from the HTTP version?
-
Library: We added
from aiohttp_socks import ProxyConnector. -
Configuration Location:
-
Before:
session.get(..., proxy=...)(configured in each request). -
After:
aiohttp.ClientSession(connector=...)(configured for the whole session).
-
-
DNS (rdns=True): The
rdns=Trueparameter does the same assocks5h— it forces domain resolution on the proxy side, hiding your destination from your provider.
This code is ideal for parsing 10,000 pages, as the connector efficiently manages the connection pool through the SOCKS tunnel.
🔧 Method 4. Using Environment Variables (Best Practice)
Professional developers do not write proxies inside their code. This is insecure (if you upload the code to GitHub, your proxies will be stolen). Python can automatically pick up proxies from the system.
In the terminal (Linux/Mac):
export HTTP_PROXY="http://user:pass@51.77.190.247:5959"
In the code:
import requests
# No proxies=... arguments need to be passed!
# The library will find the settings in the system itself.
requests.get("https://httpbin.org/ip")
💡 Which proxy should you choose for a Python script?
Code is only half the battle. The main thing is the quality of the proxy itself and the method of rotation management. Depending on the task, your script's architecture will change.
1. Server IPv4, IPv6 (Server Data Center)
Ideal for: Simple API requests, downloading files, working with sites without strict protection.
-
Logic in Python: If an IP gets banned — simply change it in the script to the next one from your proxy list.
-
Pro: High speed (up to 1 Gbps) and low price.
2. Residential Rotating
Ideal for: Aggressive parsing (Amazon, eBay), bypassing Cloudflare, and collecting large volumes of data.
-
How rotation works: You do not need to write complex rotation code. In CyberYozh App, we use a "Smart Credential Generator". You change the proxy behavior simply by changing the login:
-
login-res-any: The script gets a new IP for every request (requests.get). login-res-any-sid-12345678: The IP is fixed to this set of digits (sid) for up to 1 minute.-
login-resfix: The script fixes the IP to itself (Sticky Session) for authentication.
-
-
Pro for Python: You use just one connection string while gaining access to a million IPs.
👉 You can read more about rotation for residential proxies here.
3. Mobile Proxies (Mobile 4G/LTE)
Ideal for: Registering accounts, working with social networks (Instagram, TikTok), and emulating real users via Selenium/Appium. Highest trust level.
There are two management scenarios in the code here:
-
🅰️ Private: Rotation via link You rent the entire modem. To change the IP, your Python script must "hit" a special API link.
-
Example logic:
# 1. Perform a cycle of actions do_registration() # 2. Reboot the modem via API requests.get("https://app.cyberyozh.com/api/v1/...") time.sleep(15) # Wait for the modem to come back up # 3. Start a new cycle
-
-
🅱️ Shared: Auto-rotation The IP changes automatically based on the service timer (e.g., every 5 or 30 minutes).
- Logic in Python: You need to handle connection drops. If the modem reboots during a request, the script should catch a
ConnectionError, wait a few seconds, and retry the request (Retry).
- Logic in Python: You need to handle connection drops. If the modem reboots during a request, the script should catch a
👉 You can read more about mobile proxies here.
Conclusion
Integrating a proxy in Python takes 3-5 lines of code. The key is to choose the right proxy type for the task and the right library.
👉 Ready to start coding? In the CyberYozh App catalog, you will find all the proxy types listed. Simply choose the appropriate plan for your needs (parsing, registration, or surfing), copy the credentials, and integrate them into your project in a couple of minutes.