Proxy Authentication: Các Phương Pháp Xác Thực Proxy
Proxy services yêu cầu authentication để bảo mật. Bài viết giải thích các phương pháp xác thực proxy phổ biến.
Tại Sao Cần Authentication?
- Bảo vệ proxy khỏi unauthorized access
- Theo dõi usage per user
- Billing và quota management
- Ngăn abuse
1. Username/Password Authentication
import requests
# Format: http://username:password@host:port
proxy = {
'http': 'http://user123:pass456@proxy.vinaproxy.com:8080',
'https': 'http://user123:pass456@proxy.vinaproxy.com:8080'
}
response = requests.get('https://httpbin.org/ip', proxies=proxy)
print(response.json())
2. IP Whitelist
# Không cần username/password nếu IP đã whitelist
proxy = {
'http': 'http://proxy.vinaproxy.com:8080',
'https': 'http://proxy.vinaproxy.com:8080'
}
# Đảm bảo IP của bạn đã được add vào whitelist
response = requests.get('https://httpbin.org/ip', proxies=proxy)
3. API Key Authentication
# Một số providers dùng API key trong URL
proxy = {
'http': f'http://proxy.example.com:8080?api_key={API_KEY}',
'https': f'http://proxy.example.com:8080?api_key={API_KEY}'
}
# Hoặc trong username
proxy = {
'http': f'http://{API_KEY}:@proxy.example.com:8080'
}
Python Requests
import requests
proxies = {
'http': 'http://user:pass@proxy.vinaproxy.com:8080',
'https': 'http://user:pass@proxy.vinaproxy.com:8080'
}
# Method 1: proxies parameter
response = requests.get('https://example.com', proxies=proxies)
# Method 2: Session
session = requests.Session()
session.proxies = proxies
response = session.get('https://example.com')
Selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
PROXY = "user:pass@proxy.vinaproxy.com:8080"
chrome_options = Options()
chrome_options.add_argument(f'--proxy-server=http://{PROXY}')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://httpbin.org/ip')
Playwright
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(
proxy={
'server': 'http://proxy.vinaproxy.com:8080',
'username': 'user123',
'password': 'pass456'
}
)
page = browser.new_page()
page.goto('https://httpbin.org/ip')
print(page.content())
browser.close()
aiohttp (Async)
import aiohttp
import asyncio
async def fetch():
proxy_auth = aiohttp.BasicAuth('user123', 'pass456')
async with aiohttp.ClientSession() as session:
async with session.get(
'https://httpbin.org/ip',
proxy='http://proxy.vinaproxy.com:8080',
proxy_auth=proxy_auth
) as response:
return await response.json()
result = asyncio.run(fetch())
cURL
# Command line
curl -x http://user:pass@proxy.vinaproxy.com:8080 https://httpbin.org/ip
# With separate auth flag
curl -x http://proxy.vinaproxy.com:8080 -U user:pass https://httpbin.org/ip
Best Practices
- Không hardcode credentials trong code
- Dùng environment variables
- Rotate credentials nếu cần
- Monitor usage để detect leaks
VinaProxy Authentication
- Username/Password đơn giản
- IP Whitelist available
- Giá chỉ $0.5/GB
