Xử Lý Lỗi Proxy: Troubleshooting Guide Đầy Đủ
Proxy không hoạt động? Bài viết hướng dẫn debug và fix các lỗi proxy thường gặp.
Lỗi 1: Connection Refused
# Triệu chứng
ConnectionRefusedError: [Errno 111] Connection refused
# Nguyên nhân
- Sai host hoặc port
- Proxy server down
- Firewall chặn
# Fix
1. Kiểm tra host:port đúng chưa
2. Test với curl: curl -x http://proxy:port https://httpbin.org/ip
3. Check firewall rules
4. Liên hệ provider nếu server down
Lỗi 2: 407 Proxy Authentication Required
# Triệu chứng
HTTPError: 407 Proxy Authentication Required
# Nguyên nhân
- Sai username/password
- Credentials expired
- Format sai
# Fix
# Đảm bảo format đúng:
proxy = 'http://username:password@host:port'
# Nếu password có ký tự đặc biệt, URL encode:
from urllib.parse import quote
password = quote('p@ss#word!')
proxy = f'http://user:{password}@host:port'
Lỗi 3: Timeout
# Triệu chứng
ReadTimeout: HTTPSConnectionPool... Read timed out
# Nguyên nhân
- Proxy quá chậm
- Target site chậm
- Network issues
# Fix
# Tăng timeout
response = requests.get(url, proxies=proxies, timeout=60)
# Hoặc retry với proxy khác
for proxy in proxy_list:
try:
response = requests.get(url, proxies={'http': proxy}, timeout=30)
break
except:
continue
Lỗi 4: SSL/Certificate Error
# Triệu chứng
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED]
# Nguyên nhân
- Proxy intercepting SSL
- Certificate issues
# Fix (not recommended for production)
response = requests.get(url, proxies=proxies, verify=False)
# Better: Dùng HTTPS proxy đúng cách
proxies = {
'http': 'http://user:pass@proxy:8080',
'https': 'http://user:pass@proxy:8080' # Note: http:// for https traffic
}
Lỗi 5: 403 Forbidden (vẫn bị block)
# Triệu chứng
Dùng proxy nhưng vẫn bị 403
# Nguyên nhân
- IP đã bị blacklist
- Missing headers
- Bot detection
# Fix
1. Rotate sang IP mới
2. Thêm headers:
headers = {
'User-Agent': 'Mozilla/5.0...',
'Accept': 'text/html...',
'Accept-Language': 'en-US,en;q=0.9'
}
3. Dùng residential thay datacenter
4. Add delays giữa requests
Lỗi 6: Proxy Quá Chậm
# Benchmark proxy speed
import time
def test_proxy_speed(proxy):
start = time.time()
try:
requests.get('https://httpbin.org/ip',
proxies={'http': proxy}, timeout=10)
return time.time() - start
except:
return None
# Test và chọn proxy nhanh nhất
speeds = [(p, test_proxy_speed(p)) for p in proxy_list]
fastest = min(speeds, key=lambda x: x[1] or float('inf'))
Lỗi 7: IP Không Đổi (Rotating không work)
# Triệu chứng
IP giống nhau mọi request
# Nguyên nhân
- Session sticky
- Connection pooling
# Fix
# Tạo session mới mỗi request
response = requests.get(url, proxies=proxies) # Không dùng Session()
# Hoặc force new connection
session = requests.Session()
session.headers['Connection'] = 'close'
Debug Checklist
□ Proxy URL format đúng? (http://user:pass@host:port)
□ Credentials chính xác?
□ Host và port đúng?
□ Internet connection OK?
□ Firewall không block?
□ Proxy provider active?
□ Bandwidth còn quota?
□ Timeout đủ lớn?
Test Script
import requests
def diagnose_proxy(proxy_url):
print(f"Testing: {proxy_url[:30]}...")
try:
response = requests.get(
'https://httpbin.org/ip',
proxies={'http': proxy_url, 'https': proxy_url},
timeout=15
)
print(f"✅ Status: {response.status_code}")
print(f"✅ IP: {response.json()['origin']}")
return True
except requests.exceptions.ProxyError as e:
print(f"❌ Proxy Error: {e}")
except requests.exceptions.Timeout:
print(f"❌ Timeout")
except Exception as e:
print(f"❌ Error: {e}")
return False
diagnose_proxy('http://user:pass@proxy.vinaproxy.com:8080')
VinaProxy Support
- 24/7 technical support
- Tiếng Việt assistance
- Giá chỉ $0.5/GB
