Proxy as a class
- The
Proxy.from_str()method supports most proxy formats (with and without protocol):host:port:login:password host:port@login:password login:password@host:port login:password:host:port host:port - The
Proxy.from_file()method returns the list of proxies from the file at the specified path
pip install better-proxyimport aiohttp
from better_proxy import Proxy
from aiohttp_socks import ProxyConnector
proxy = Proxy.from_str("socks5://user:[email protected]:1080")
async def fetch(url):
connector = ProxyConnector.from_url(proxy.as_url)
async with aiohttp.ClientSession(connector=connector) as session:
async with session.get(url) as response:
return await response.text()import requests
from better_proxy import Proxy
proxy = Proxy.from_str("http://user:password@host:port")
def fetch(url):
response = requests.get(url, proxies=proxy.as_proxies_dict)
return response.textfrom playwright.async_api import async_playwright, Playwright
from better_proxy import Proxy
proxy = Proxy.from_str("http://user:password@host:port")
async def fetch(playwright: Playwright, url):
chromium = playwright.chromium
browser = await chromium.launch(proxy=proxy.as_playwright_proxy)
...import httpx
from better_proxy import Proxy
proxy = Proxy.from_str("login:[email protected]:3001")
async def fetch(url):
async with httpx.AsyncClient(proxy=proxy.as_url) as client:
response = await client.get(url)
return response.textimport httpx
from httpx_socks import AsyncProxyTransport
from better_proxy import Proxy
proxy = Proxy.from_str("socks5://login:[email protected]:3001")
async def fetch(url):
transport = AsyncProxyTransport.from_url(proxy.as_url)
async with httpx.AsyncClient(transport=transport) as client:
response = await client.get(url)
return response.textimport httpx
from better_proxy import Proxy
proxy = Proxy.from_str('res.proxy-seller.io:10071:login:password')
def print_proxy_and_ip(proxy: Proxy):
response = httpx.get('https://api.myip.com', proxy=proxy.as_url)
print(proxy, response.text)
if __name__ == '__main__':
print_proxy_and_ip(proxy)
proxy.increment_port()
print_proxy_and_ip(proxy)import httpx
from better_proxy import Proxy
proxy = Proxy.from_str('http://proxy-sid-m563x6cqwvomn4m-filter-medium:[email protected]:8080')
def print_proxy_and_ip(proxy: Proxy):
response = httpx.get('https://api.myip.com', proxy=proxy.as_url)
print(proxy, response.text)
if __name__ == '__main__':
print_proxy_and_ip(proxy)
proxy.randomized_nodemaven_sid()
print_proxy_and_ip(proxy)