### Installing ProxyStr Library (Bash) Source: https://github.com/botsforge/proxystr/blob/main/README.md This command shows the standard method for installing the `proxystr` library and its dependencies using the pip package manager in a bash or shell environment. ```bash pip install proxystr ``` -------------------------------- ### Retrieving Proxy Information in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Demonstrates how to obtain geographical and network information about proxies. It shows how to get info for multiple proxies during a check using `check_proxies(..., with_info=True)` and how to get info for a single proxy using the `proxy.get_info()` method. ```python from proxystr import Proxy, check_proxies proxies = [ Proxy("login:password@210.173.88.77:3001"), Proxy("login:password@210.173.88.78:3002") ] good_proxies, bad_proxies = check_proxies(proxies, with_info=True) for proxy, info in good_proxies: print(info) # or for single proxy: proxy = Proxy("login:password@210.173.88.77:3001") print(proxy.get_info()) ``` -------------------------------- ### Using ProxyStr with Httpx (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This section provides multiple examples showing how to integrate a `proxystr.Proxy` object with the `httpx` library for both asynchronous and synchronous HTTP requests using the `proxy.url` attribute. Requires `httpx`. ```python import httpx from proxystr import Proxy proxy = Proxy("login:password@210.173.88.77:3001") async def fetch(url): async with httpx.AsyncClient(proxy=proxy.url, follow_redirects=True) as client: response = await client.get(url) return response.text # or def sync_fetch(url): with httpx.Client(proxy=proxy.url, follow_redirects=True) as client: return client.get(url).text # or def simple_fetch(url): return httpx.get(url, proxy=proxy.url, follow_redirects=True).text ``` -------------------------------- ### Creating Mobile Proxy Objects with Refresh URL (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md These examples demonstrate how to include a specific refresh or rotation URL for mobile proxies within the proxy string format by appending the URL in square brackets. ```python Proxy('host:port:login:password[https://rotate.my-proxy.io?api_key=your_api_key]') Proxy('http://login:password@host:port[https://rotate.my-proxy.io?api_key=your_api_key]') ``` -------------------------------- ### Creating Proxy Objects with Various Formats (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This code block illustrates the flexibility of the `Proxy` constructor, showing how it can parse various string formats for defining proxy connections, including those with and without credentials. ```python from proxystr import Proxy Proxy('host:port') Proxy('host:port:login:password') Proxy('login:password@host:port') Proxy('login:password|host:port') Proxy('http://login:password@host:port') ``` -------------------------------- ### Implementing Sync Fetch with Proxy in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Demonstrates how to use a `Proxy` object within a synchronous fetch function. It shows two methods: using a generic `Client` with the proxy directly or obtaining a client specifically configured for the proxy via `proxy.get_client()`. Requires a `Client` class (not shown, assumed from context) and the `proxystr.Proxy` object. ```python def sync_fetch(url): with Client(proxy=proxy) as client: return client.get(url).text # or with proxy.get_client() as client: return client.get(url).text ``` -------------------------------- ### Using ProxyStr Built-in Httpx Clients (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This asynchronous function illustrates how to use the `Client` and `AsyncClient` classes provided directly by `proxystr`, which wrap `httpx` and simplify proxy usage by accepting a `Proxy` object directly. Requires `httpx`. ```python from proxystr import Proxy, Client, AsyncClient proxy = Proxy("log:pass@210.173.88.77:3001") # or "socks5://log:pass@210.173.88.77:3001" async def fetch(url): async with AsyncClient(proxy=proxy) as client: response = await client.get(url) return response.text # or async with proxy.get_async_client() as client: response = await client.get(url) return response.text ``` -------------------------------- ### Using ProxyStr with Aiohttp-Socks (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This asynchronous function demonstrates how to use a `proxystr.Proxy` object with the `aiohttp-socks` library to handle SOCKS proxies by creating a `ProxyConnector` from the `proxy.url` attribute. Requires `aiohttp` and `aiohttp-socks`. ```python import aiohttp from aiohttp_socks import ProxyConnector from proxystr import Proxy proxy = Proxy("socks5://login:password@210.173.88.77:3001") async def fetch(url): connector = ProxyConnector.from_url(proxy.url) async with aiohttp.ClientSession(connector=connector) as session: async with session.get(url) as response: return await response.text() ``` -------------------------------- ### Using ProxyStr with Requests (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This synchronous function illustrates how to use a `proxystr.Proxy` object with the popular `requests` library by passing the proxy configuration dictionary obtained from `proxy.dict` to the `proxies` parameter. Requires `requests`. ```python import requests from proxystr import Proxy proxy = Proxy("login:password@210.173.88.77:3001") def fetch(url): response = requests.get(url, proxies=proxy.dict) return response.text ``` -------------------------------- ### Integrating Proxy with Playwright in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Shows how to configure Playwright's Chromium browser to use a proxy provided by `proxystr`. It requires importing `async_playwright` and `Playwright`, creating a `Proxy` object, and passing `proxy.playwright` to the `browser.launch()` method. This is for asynchronous usage. ```python from playwright.async_api import async_playwright, Playwright from proxystr import Proxy proxy = Proxy("login:password@210.173.88.77:3001") async def fetch(playwright: Playwright, url): chromium = playwright.chromium browser = await chromium.launch(proxy=proxy.playwright) ... ``` -------------------------------- ### Using ProxyStr with Httpx-Socks (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This section demonstrates how to use a `proxystr.Proxy` object with the `httpx-socks` library to handle SOCKS proxies in both asynchronous and synchronous contexts by creating transport objects from the `proxy.url` attribute. Requires `httpx` and `httpx-socks`. ```python import httpx from httpx_socks import AsyncProxyTransport, SyncProxyTransport from proxystr import Proxy proxy = Proxy("socks5://login:password@210.173.88.77:3001") async def fetch(url): transport = AsyncProxyTransport.from_url(proxy.url) async with httpx.AsyncClient(transport=transport, follow_redirects=True) as client: response = await client.get(url) return response.text # or def sync_fetch(url): transport = SyncProxyTransport.from_url(proxy.url) with httpx.Client(transport=transport, follow_redirects=True) as client: return client.get(url).text ``` -------------------------------- ### Using ProxyStr with Aiohttp (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This asynchronous function shows how to integrate a `proxystr.Proxy` object with the `aiohttp` library to make HTTP requests routed through the specified proxy using the `proxy.url` attribute. Requires `aiohttp`. ```python import aiohttp from proxystr import Proxy proxy = Proxy("login:password@210.173.88.77:3001") async def fetch(url): async with aiohttp.ClientSession() as session: async with session.get(url, proxy=proxy.url) as response: return await response.text() ``` -------------------------------- ### Checking Proxy Object String Behavior (Python) Source: https://github.com/botsforge/proxystr/blob/main/README.md This snippet demonstrates how to use the `isinstance()` function to verify that an instance of the `Proxy` class from the `proxystr` library behaves like a standard Python string. ```python isinstance(Proxy('127.0.0.1:3001'), str) # --> True ``` -------------------------------- ### Using Proxy Objects with Pydantic Models in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Shows how `proxystr.Proxy` objects can be used as fields within Pydantic models. It demonstrates that Pydantic can automatically parse both `Proxy` instances and raw proxy strings into `Proxy` objects when creating model instances. ```python from proxystr import Proxy from pydantic import BaseModel class Account(BaseModel): number: int proxy: Proxy | None = None for account in [ Account(number=1, proxy=Proxy('login:password@210.173.88.77:3001')), Account(number=2, proxy='login:password@210.173.88.77:3001') ]: print(account) print(account.model_dump()) ``` -------------------------------- ### Proxy Object Equality and Set Support in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Illustrates how `Proxy` objects handle equality comparisons (`==`) and support inclusion in sets. It shows that two `Proxy` objects representing the same proxy address are considered equal (`== True`) even if they are different instances (`is False`), and that sets correctly treat them as the same unique element. ```python from proxystr import Proxy p1 = Proxy('login:password@210.173.88.77:3001') p2 = Proxy('210.173.88.77:3001:login:password') print(p1 == '210.173.88.77:3001:login:password') print(p1 == p2) print(p1 is p2) print(set((p1, p2))) ``` -------------------------------- ### Checking Proxy Validity in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Shows how to check the validity of one or more proxies using `proxystr`. It covers checking a list of `Proxy` objects, a list of raw proxy strings, proxies read from a file, and checking a single `Proxy` object using its `.check()` method. ```python from proxystr import Proxy, check_proxies, read_proxies proxies = [ Proxy("login:password@210.173.88.77:3001"), Proxy("login:password@210.173.88.78:3002") ] good_proxies, bad_proxies = check_proxies(proxies) # or in raw str format: good_proxies, bad_proxies = check_proxies(["log:pass@210.173.88.77:3001", "log:pass@210.173.88.78:3002"]) # or read from file and check good_proxies, bad_proxies = check_proxies(read_proxies('proxies.txt')) # or for single proxy: proxy = Proxy("login:password@210.173.88.77:3001") if proxy.check(): '''do_something''' ``` -------------------------------- ### Accessing Proxy Object Properties in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Illustrates various ways to represent and access data from a `Proxy` object. It shows printing the object directly, serializing it to JSON, embedding it in a dictionary, and accessing specific attributes like `url`, `dict`, and `json()`. It also shows accessing the playwright-compatible format. ```python import json from proxystr import Proxy proxy = Proxy("login:password@210.173.88.77:3001") print(proxy) print(json.dumps(proxy)) print({'proxy': proxy}) print(proxy.url) print(proxy.dict) print(proxy.json()) print(proxy.playwright()) ``` -------------------------------- ### Setting Default Proxy String Pattern in Python Source: https://github.com/botsforge/proxystr/blob/main/README.md Demonstrates how to change the default string representation pattern for `Proxy` objects using `Proxy.set_default_pattern()`. It shows how a proxy string with a rotation URL is parsed and represented according to the new pattern. ```python import json from proxystr import Proxy Proxy.set_default_pattern('protocol://ip:port:username:password[rotation_url]') proxy = Proxy("login:password@210.173.88.77:3001[https://rotate.my-proxy.io?api_key=your_api_key]") print(proxy) print(json.dumps(proxy)) print({'proxy': proxy}) ``` -------------------------------- ### Project Dependencies - Python Source: https://github.com/botsforge/proxystr/blob/main/requirements.txt Specifies the core Python packages and their minimum required versions for the project to function correctly. These dependencies are typically managed using tools like pip. ```Python pydantic>=2.0 httpx>=0.27 httpx-socks>=0.9 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.