Skip to content

Smart Proxy Manager

The SmartProxy class wraps ASocksClient to provide a high-level proxy pool with automatic rotation, health checking, and self-healing.

  1. Initialize a pool of proxies matching your criteria (country, protocol, type).
  2. Request a proxySmartProxy returns the next healthy proxy URL.
  3. Auto-heal — if a proxy fails, it is automatically replaced. No fatal errors are raised.
import asyncio
from asockslib import ASocksClient, SmartProxy
async def main():
async with ASocksClient(api_key="sk-...") as client:
smart = SmartProxy(client, country_code="US")
await smart.initialize(pool_size=5)
proxy_url = await smart.get_proxy()
print(proxy_url) # socks5://user:pass@host:port
asyncio.run(main())
urls = await smart.get_all_proxies()
results = await smart.health_check_all()
# {1001: True, 1002: False, 1003: True}
replaced = await smart.refresh_pool()
print(f"{replaced} proxies were replaced")
ParameterTypeDefaultDescription
clientASocksClientrequiredThe API client instance
country_codestr""ISO country code filter
citystr""City filter
health_check_urlstrhttps://api.ipify.org?format=jsonURL used for health checks
health_timeoutfloat10.0Health check timeout in seconds
import httpx
from asockslib import ASocksClient, SmartProxy
async def scrape():
async with ASocksClient(api_key="sk-...") as client:
smart = SmartProxy(client, country="US")
await smart.initialize(pool_size=3)
proxy_url = await smart.get_proxy()
async with httpx.AsyncClient(proxy=proxy_url) as http:
resp = await http.get("https://example.com")
print(resp.status_code)
from asockslib.exceptions import NoAvailableProxyError
try:
proxy = await smart.get_proxy()
except NoAvailableProxyError:
print("All proxies failed — check your account or criteria")