ProxyServer¶
The core immutable data structure representing a proxy server connection.
proxy_string.base.ProxyServer
¶
Immutable representation of proxy server connection data and credentials.
Attributes:
| Name | Type | Description |
|---|---|---|
host |
str
|
Hostname or IP address of the proxy server. |
port |
int
|
Connection port number (0..65535). |
protocol |
ProxyServerProtocol
|
Proxy network protocol (HTTP, HTTPS, SOCKS4, SOCKS5). |
username |
str | None
|
Optional authentication username. |
password |
str | None
|
Optional authentication password. |
remote_dns |
bool | None
|
Whether DNS resolution should occur on the proxy server. |
unicode_host |
str
|
Unicode representation of the host (for IDN/punycode). |
host_value_type |
HostValueType
|
Host classification (HOSTNAME, IPV4, or IPV6). |
_host_value_type
class-attribute
instance-attribute
¶
_host_value_type: HostValueType | None = field(
default=None,
validator=validators.optional(
validators.instance_of(HostValueType)
),
eq=False,
hash=False,
repr=False,
kw_only=True,
)
_skip_validation
class-attribute
instance-attribute
¶
_skip_validation: bool = field(
default=False,
validator=validators.instance_of(bool),
eq=False,
hash=False,
repr=False,
kw_only=True,
)
host
class-attribute
instance-attribute
¶
host: str = field(
validator=[
validators.instance_of(str),
validators.min_len(1),
],
eq=True,
hash=True,
repr=True,
)
host_ip_address
property
¶
Returns the IP address object if the host is an IP, otherwise None.
Returns:
| Type | Description |
|---|---|
IPv4Address | IPv6Address | None
|
IPv4Address | IPv6Address | None: Parsed IP address object or None. |
host_value_type
property
¶
host_value_type: HostValueType
Returns the classification of the host (HOSTNAME, IPV4, or IPV6).
Returns:
| Name | Type | Description |
|---|---|---|
HostValueType |
HostValueType
|
Host classification enum member. |
is_authenticated
property
¶
Returns True if the proxy has credentials (username or password).
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if username or password is set, False otherwise. |
is_socks
property
¶
Returns True if the proxy protocol is a SOCKS protocol (SOCKS4 or SOCKS5).
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if protocol is SOCKS4 or SOCKS5, False otherwise. |
is_ssl
property
¶
Returns True if the proxy connection uses TLS/SSL (HTTPS).
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if protocol is HTTPS, False otherwise. |
origin
property
¶
Returns the origin URL excluding authentication credentials.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Proxy origin URL without credentials. |
password
class-attribute
instance-attribute
¶
password: str | None = field(
default=None,
validator=validators.optional(
validators.instance_of(str)
),
eq=True,
hash=True,
repr=lambda v: "'***'" if v is not None else "None",
kw_only=True,
)
port
class-attribute
instance-attribute
¶
port: int = field(
default=0,
converter=int,
validator=[validators.ge(0), validators.le(65535)],
eq=True,
hash=True,
repr=True,
)
protocol
class-attribute
instance-attribute
¶
protocol: ProxyServerProtocol = field(
default=ProxyServerProtocol.HTTP,
validator=_validate_protocol,
eq=True,
hash=True,
repr=True,
kw_only=True,
)
remote_dns
class-attribute
instance-attribute
¶
remote_dns: bool | None = field(
default=None,
validator=validators.optional(
validators.instance_of(bool)
),
eq=True,
hash=True,
repr=True,
kw_only=True,
)
unicode_host
property
¶
Returns the host decoded from IDNA Punycode to Unicode if applicable.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Unicode representation of the host name. |
unicode_url
property
¶
Returns the URL representation with Unicode host and credentials.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Proxy URL with decoded Unicode hostname. |
url
property
¶
Returns the full URL representation including credentials.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Absolute proxy URL. |
username
class-attribute
instance-attribute
¶
username: str | None = field(
default=None,
validator=validators.optional(
validators.instance_of(str)
),
eq=True,
hash=True,
repr=lambda v: repr(v) if v is not None else "None",
kw_only=True,
)
__attrs_post_init__
¶
Sets default port and resolves default remote_dns flag for the protocol.
Source code in src/proxy_string/base.py
__str__
¶
from_dict
classmethod
¶
Creates a ProxyServer instance from a dictionary of parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dict
|
Mapping[str, Any]
|
Dictionary containing proxy configuration keys ('host', 'port', 'protocol' or 'scheme', 'username', 'password', 'remote_dns'). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ProxyServer |
Self
|
A new ProxyServer instance. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input_dict is not a Mapping. |
ProxyConfigurationError
|
If 'host' is missing or empty. |
Source code in src/proxy_string/base.py
from_file
classmethod
¶
from_file(
input_file: str | Path,
*,
string_format: ProxyStringFormat | None = None,
protocol: ProxyServerProtocol | None = None,
encoding: str = "utf-8",
encoding_errors: str = "strict",
ignore_invalid: bool = False,
) -> Generator[Self, None, None]
Yields ProxyServer instances by reading proxy strings from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
str | Path
|
Path to the proxy text file. |
required |
string_format
|
ProxyStringFormat | None
|
Optional explicit ProxyStringFormat for rows. Recommended when usernames or passwords are numeric to avoid port ambiguity in 4-part colon-delimited strings. |
None
|
protocol
|
ProxyServerProtocol | None
|
Optional protocol override for all rows. |
None
|
encoding
|
str
|
Text encoding of the file (defaults to 'utf-8'). |
'utf-8'
|
encoding_errors
|
str
|
Text codec error handling mode passed to open() (defaults to 'strict'). |
'strict'
|
ignore_invalid
|
bool
|
If True, rows that cannot be parsed are skipped. If False (default), raises InvalidProxyStringError or ValueError. |
False
|
Yields:
| Name | Type | Description |
|---|---|---|
ProxyServer |
Self
|
Parsed proxy server instances. |
Raises:
| Type | Description |
|---|---|
InvalidProxyStringError
|
If a row cannot be parsed and ignore_invalid is False. |
Source code in src/proxy_string/base.py
from_file_async
async
classmethod
¶
from_file_async(
input_file: str | Path,
*,
string_format: ProxyStringFormat | None = None,
protocol: ProxyServerProtocol | None = None,
encoding: str = "utf-8",
encoding_errors: str = "strict",
ignore_invalid: bool = False,
) -> AsyncGenerator[Self, None]
Asynchronously yields ProxyServer instances by reading a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
str | Path
|
Path to the proxy text file. |
required |
string_format
|
ProxyStringFormat | None
|
Optional explicit ProxyStringFormat for rows. Recommended when usernames or passwords are numeric to avoid port ambiguity in 4-part colon-delimited strings. |
None
|
protocol
|
ProxyServerProtocol | None
|
Optional protocol override for all rows. |
None
|
encoding
|
str
|
Text encoding of the file (defaults to 'utf-8'). |
'utf-8'
|
encoding_errors
|
str
|
Text codec error handling mode passed to open() (defaults to 'strict'). |
'strict'
|
ignore_invalid
|
bool
|
If True, rows that cannot be parsed are skipped. If False (default), raises InvalidProxyStringError or ValueError. |
False
|
Yields:
| Name | Type | Description |
|---|---|---|
ProxyServer |
AsyncGenerator[Self, None]
|
Parsed proxy server instances. |
Raises:
| Type | Description |
|---|---|
InvalidProxyStringError
|
If a row cannot be parsed and ignore_invalid is False. |
Source code in src/proxy_string/base.py
from_json
classmethod
¶
Creates a ProxyServer instance from a JSON-formatted string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_json
|
str
|
JSON string representing proxy parameters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ProxyServer |
Self
|
A new ProxyServer instance. |
Raises:
| Type | Description |
|---|---|
JSONDecodeError
|
If input_json is not valid JSON. |
Source code in src/proxy_string/base.py
from_string
classmethod
¶
from_string(
proxy_string: str,
*,
string_format: ProxyStringFormat | None = None,
protocol: ProxyServerProtocol | None = None,
) -> Self
Creates a ProxyServer instance by parsing a formatted proxy string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proxy_string
|
str
|
Formatted proxy string (e.g. 'host:port', 'user:pass@host:port'). |
required |
string_format
|
ProxyStringFormat | None
|
Optional explicit ProxyStringFormat to match against. Recommended when usernames or passwords are numeric to avoid port ambiguity in 4-part colon-delimited strings. |
None
|
protocol
|
ProxyServerProtocol | None
|
Optional protocol override (defaults to parsed protocol or HTTP). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ProxyServer |
Self
|
A new ProxyServer instance. |
Raises:
| Type | Description |
|---|---|
InvalidProxyStringError
|
If the string does not match the expected format. |
Source code in src/proxy_string/base.py
from_url
classmethod
¶
Creates a ProxyServer instance by parsing a proxy URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
A complete proxy URL (e.g. 'http://user:pass@proxy.com:8080'). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ProxyServer |
Self
|
A new ProxyServer instance. |
Raises:
| Type | Description |
|---|---|
InvalidProxyURLError
|
If the URL format, scheme, or host is invalid. |
Source code in src/proxy_string/base.py
prepare_for
¶
prepare_for(
package_name: Literal[
"urllib3",
"httpx",
"httpx2",
"aiohttp",
"pycurl",
"curl_cffi",
],
) -> str
prepare_for(
package_name: SupportedPackage | str,
) -> dict[str, Any] | str
prepare_for(
package_name: SupportedPackage | str,
) -> dict[str, Any] | str
Prepares proxy configuration for third-party libraries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
SupportedPackage | str
|
Target library name ('urllib', 'requests', 'urllib3', 'httpx', 'httpx2', 'aiohttp', 'playwright', 'pycurl', or 'curl_cffi'). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | str
|
dict[str, Any] | str: Library-specific proxy configuration structure. |
Raises:
| Type | Description |
|---|---|
UnsupportedPackageError
|
If the package is not supported. |
ProtocolNotSupportedError
|
If the protocol is incompatible. |
Source code in src/proxy_string/base.py
to_dict
¶
Serializes proxy server attributes into a dictionary.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
Dictionary with host, port, protocol, username, password, remote_dns. |
Source code in src/proxy_string/base.py
to_json
¶
Serializes proxy server attributes into a JSON string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
JSON representation of the proxy server. |
to_string
¶
to_string(
string_format: ProxyStringFormat | None = None,
) -> str
Formats the proxy server into a specified or auto-selected string format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string_format
|
ProxyStringFormat | None
|
Format template from ProxyStringFormat. If None, defaults to HOST_PORT_USER_PASS when credentials are present, otherwise HOST_PORT. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted proxy string. |
Source code in src/proxy_string/base.py
to_url
¶
Serializes proxy server to URL string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
socks_ext
|
bool
|
Whether to use extended SOCKS schemes ('socks4a', 'socks5h'). |
True
|
exclude_credentials
|
bool
|
If True, omits username and password from the URL. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Proxy server URL. |
Source code in src/proxy_string/base.py
with_creds
¶
Returns a copy of the proxy with modified credentials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str | None
|
The new username credential. |
required |
password
|
str | None
|
The new password credential. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new proxy server instance. |
Source code in src/proxy_string/base.py
with_host
¶
Returns a copy of the proxy with a modified host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The new host name or IP address. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new proxy server instance. |
with_password
¶
Returns a copy of the proxy with a modified password.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str | None
|
The new password credential. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new proxy server instance. |
Source code in src/proxy_string/base.py
with_port
¶
Returns a copy of the proxy with a modified port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
int
|
The new port number. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new proxy server instance. |
with_protocol
¶
with_protocol(protocol: ProxyServerProtocol | str) -> Self
Returns a copy of the proxy with a modified protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
protocol
|
ProxyServerProtocol | str
|
The new proxy protocol enum member or protocol name string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new proxy server instance. |
Source code in src/proxy_string/base.py
with_remote_dns
¶
Returns a copy of the proxy with a modified remote_dns flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_dns
|
bool | None
|
Whether DNS resolution should occur on the proxy. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new proxy server instance. |
Source code in src/proxy_string/base.py
with_username
¶
Returns a copy of the proxy with a modified username.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str | None
|
The new username credential. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new proxy server instance. |
Source code in src/proxy_string/base.py
proxy_string.base.SupportedPackage
module-attribute
¶
SupportedPackage = Literal[
"aiohttp",
"curl_cffi",
"httpx",
"httpx2",
"playwright",
"pycurl",
"requests",
"urllib",
"urllib3",
]