Tích Hợp Python
Cập nhật: Tháng 5, 2026
Tích hợp DanCuProxy với Python qua requests, aiohttp và xử lý lỗi đúng cách.
HTTP Proxy (requests)
Cách đơn giản nhất dùng proxy trong Python:
import requests
proxies = {
"http":
"http://user:[email protected]:1337",
"https":
"http://user:[email protected]:1337",
}
response = requests.get(
"https://api.ipify.org", proxies=proxies)
print(response.text)
SOCKS5 Proxy
Để hỗ trợ SOCKS5, cài gói requests[socks]:
import requests
proxies = {
"http":
"socks5://user:[email protected]:1338",
"https":
"socks5://user:[email protected]:1338",
}
response = requests.get(
"https://api.ipify.org", proxies=proxies)
Async với aiohttp
Cho thao tác async hiệu suất cao, dùng aiohttp:
import aiohttp
import asyncio
async def fetch():
async with aiohttp.ClientSession()
as session:
proxy =
"http://user:[email protected]:1337"
async with session.get(
"https://api.ipify.org", proxy=proxy)
as resp:
print(
await resp.text())
asyncio.run(fetch())
Xử Lý Lỗi
Luôn xử lý lỗi cho kết nối proxy:
import requests
from requests.exceptions import ProxyError, ConnectTimeout
try:
response = requests.get("https://api.ipify.org",
proxies=proxies, timeout=10)
except ProxyError:
print("Check proxy credentials")
except ConnectTimeout:
print("Proxy connection timed out")
Đặt timeout (10-30s) cho mọi yêu cầu proxy. Không có timeout, kết nối thất bại sẽ treo vô thời hạn.