ProxyProviders Models

class proxyproviders.models.proxy.Proxy(id: str, username: str, password: str, proxy_address: str, port: int, country_code: str | None = None, city_name: str | None = None, created_at: datetime | None = None, protocols: List[str] | None = None)[source]

Bases: object

Our shared data model for a proxy object across all providers.

id: str

A unique identifier for the proxy

username: str

The username required for authenticating with the proxy

password: str

The password required for authenticating with the proxy. Left out of repr() so printing or logging a proxy does not expose it.

proxy_address: str

The IP address or domain name of the proxy

port: int

The port number through which the proxy connection is established

country_code: str | None = None

The country code where the proxy is located, e.g., ‘US’, ‘FR’. Optional

city_name: str | None = None

The city name where the proxy is located, e.g., ‘New York’, ‘Paris’. Optional

created_at: datetime | None = None

The timestamp when the proxy was created. Optional

protocols: List[str] | None = None

A list of connection protocols supported by the proxy, e.g., [‘http’, ‘https’]

classmethod from_string(text: str, id: str | None = None, **fields: Any) → Proxy[source]

Build a proxy from text, for proxies you already have as a list or a file.

Accepted forms:

  • a URL: http://user:pass@host:port, socks5://host:port

  • user:pass@host:port and host:port (treated as http)

  • host:port:user:pass, the line format many providers export

Parameters:
  • text – The proxy to parse

  • id – Identifier for the proxy. Defaults to host:port.

  • fields – Any other Proxy fields to set, e.g. country_code="US"

Raises:

ValueError – If the text is not one of the accepted forms

Example

>>> Proxy.from_string("203.0.113.7:8080:user:pass").to_url()
'http://user:pass@203.0.113.7:8080'
classmethod from_url(url: str, id: str | None = None, **fields: Any) → Proxy[source]

Build a proxy from a URL such as http://user:pass@host:port.

The reverse of to_url(): percent-encoded credentials are decoded. See from_string(), which this is an alias of, for every accepted form.

property scheme: str

The URL scheme used to connect to this proxy.

protocols lists what the proxy supports. A proxy that supports http or https (or does not say) is reached over http. A SOCKS-only proxy, e.g. protocols=["socks5"], is reached over that SOCKS scheme.

to_url(protocol: str | None = None) → str[source]

Convert proxy to URL format for use with HTTP clients.

Parameters:

protocol – The protocol to use in the URL. Defaults to scheme, which is http unless the proxy only supports SOCKS.

Returns:

Proxy URL in format ‘protocol://username:password@host:port’. The username and password are percent-encoded, so characters such as @, :, # or / in them do not change where the URL points. A proxy with no username and no password gives ‘protocol://host:port’.

Example

>>> proxy.to_url()
'http://user:pass@192.168.1.1:8080'
>>> proxy.to_url('https')
'https://user:pass@192.168.1.1:8080'
format(format_type: Literal[ProxyFormat.URL, ProxyFormat.HTTPX, ProxyFormat.AIOHTTP, ProxyFormat.SCRAPY, ProxyFormat.HOST_PORT_USER_PASS, 'url', 'httpx', 'aiohttp', 'scrapy', 'host_port_user_pass'] = ProxyFormat.URL, **kwargs: Any) → str[source]
format(format_type: Literal[ProxyFormat.REQUESTS, ProxyFormat.URLLIB, ProxyFormat.PLAYWRIGHT, ProxyFormat.ENV, 'requests', 'urllib', 'playwright', 'env'], **kwargs: Any) → Dict[str, str]
format(format_type: Literal[ProxyFormat.CURL, 'curl'], **kwargs: Any) → List[str]
format(format_type: ProxyFormat | str, **kwargs: Any) → str | Dict[str, str] | List[str]

Format proxy for different HTTP clients and tools.

A SOCKS-only proxy (protocols=["socks5"]) is formatted with its SOCKS scheme. Your HTTP client needs SOCKS support for that: requests[socks] or httpx[socks]. aiohttp and Scrapy have no built-in SOCKS support.

Parameters:
  • format_type – Output format (default: URL string). See ProxyFormat for every format and what it is passed to.

  • kwargs – Format-specific options: protocol for URL and PLAYWRIGHT, protocols for REQUESTS and URLLIB

Returns:

Formatted proxy data

Examples

>>> proxy.format()  # Default URL string
'http://user:pass@192.168.1.1:8080'
>>> proxy.format(ProxyFormat.REQUESTS)  # requests.get(url, proxies=...)
{'http': 'http://user:pass@192.168.1.1:8080', 'https': 'http://user:pass@192.168.1.1:8080'}
>>> proxy.format('curl')  # String also works
['-x', 'http://user:pass@192.168.1.1:8080']
>>> proxy.format(ProxyFormat.HTTPX)  # httpx.Client(proxy=...)
'http://user:pass@192.168.1.1:8080'
>>> proxy.format(ProxyFormat.PLAYWRIGHT)  # browser.new_context(proxy=...)
{'server': 'http://192.168.1.1:8080', 'username': 'user', 'password': 'pass'}
>>> proxy.format(ProxyFormat.HOST_PORT_USER_PASS)
'192.168.1.1:8080:user:pass'
class proxyproviders.models.proxy.ProxyFormat(*values)[source]

Bases: Enum

Supported proxy output formats.

REQUESTS = 'requests'

{"http": url, "https": url} for the proxies= argument of requests

CURL = 'curl'

["-x", url] arguments for the curl command line

HTTPX = 'httpx'

URL string for the proxy= argument of httpx.Client / httpx.AsyncClient

AIOHTTP = 'aiohttp'

URL string for the proxy= argument of aiohttp requests and sessions

PLAYWRIGHT = 'playwright'

{"server", "username", "password"} for the proxy= option of Playwright

URL = 'url'

scheme://username:password@host:port URL string

SCRAPY = 'scrapy'

URL string for Request(meta={"proxy": ...}) in Scrapy

URLLIB = 'urllib'

{"http": url, "https": url} for urllib.request.ProxyHandler

ENV = 'env'

HTTP_PROXY / HTTPS_PROXY (and ALL_PROXY for SOCKS) environment variables, in upper and lower case, for subprocesses and tools that read them

HOST_PORT_USER_PASS = 'host_port_user_pass'

host:port:username:password, the plain-text line many proxy tools import

Proxy Formats

Converters between Proxy and the shapes that HTTP clients, browsers and tools expect.

Each output format is one small function registered in _FORMATTERS. Adding a format means adding a ProxyFormat member and a function here; nothing else changes. Parsing the other way (text to proxy fields) lives in parse_proxy_string().

class proxyproviders.models.formats.ProxyFormat(*values)[source]

Bases: Enum

Supported proxy output formats.

REQUESTS = 'requests'

{"http": url, "https": url} for the proxies= argument of requests

CURL = 'curl'

["-x", url] arguments for the curl command line

HTTPX = 'httpx'

URL string for the proxy= argument of httpx.Client / httpx.AsyncClient

AIOHTTP = 'aiohttp'

URL string for the proxy= argument of aiohttp requests and sessions

PLAYWRIGHT = 'playwright'

{"server", "username", "password"} for the proxy= option of Playwright

URL = 'url'

scheme://username:password@host:port URL string

SCRAPY = 'scrapy'

URL string for Request(meta={"proxy": ...}) in Scrapy

URLLIB = 'urllib'

{"http": url, "https": url} for urllib.request.ProxyHandler

ENV = 'env'

HTTP_PROXY / HTTPS_PROXY (and ALL_PROXY for SOCKS) environment variables, in upper and lower case, for subprocesses and tools that read them

HOST_PORT_USER_PASS = 'host_port_user_pass'

host:port:username:password, the plain-text line many proxy tools import

proxyproviders.models.formats.format_proxy(proxy: Proxy, format_type: ProxyFormat | str, **kwargs: Any) → str | Dict[str, str] | List[str][source]

Format proxy as format_type. See Proxy.format().

proxyproviders.models.formats.parse_proxy_string(text: str) → Dict[str, Any][source]

Parse one proxy written as text into the fields of a Proxy.

Accepted forms:

  • a URL: http://user:pass@host:port, socks5://host:port

  • host:port:user:pass, the line format many providers export, and host:port

  • user:pass@host:port (treated as http)

Percent-encoded credentials in a URL are decoded. In host:port:user:pass the password is everything after the third colon, so it may itself contain : or @. An IPv6 host is written in square brackets, e.g. [2001:db8::1]:8080.

Raises:

ValueError – if the text is not one of these forms