Utilities
BaseProxy
- class auto_proxy_vpn.utils.base_proxy.BaseProxy[source]
Bases:
ABC- get_proxy() dict[str, str] | None[source]
Returns the proxy in a format that can be used by the requests library. Empty dict if no public IP yet.
- is_active(wait: bool = False) bool[source]
Checks if proxy is active.
- Parameters:
wait (bool) – Always wait until the proxy is active, even if it is asynchronous.
- close(wait: bool = True)[source]
Closes the proxy, destroying it if on_exit is set to ‘destroy’ or keeping it if on_exit is set to ‘keep’.
- Parameters:
wait (bool) – Whether to wait until the proxy is fully removed when closing it. If the proxy is not asynchronous and wait is False, it will return immediately without waiting for the proxy to be fully removed. For asynchronous proxies, this parameter has no effect and the method will never wait for the proxy to be fully removed before returning.
ProxyBatch
- class auto_proxy_vpn.utils.base_proxy.ProxyBatch(proxies: list[T])[source]
Bases:
Generic[T]- __init__(proxies: list[T])[source]
Container for a group of proxies with iteration and lifecycle control.
The batch shuffles the incoming proxies on creation to avoid predictable ordering. It behaves as an iterable and iterator, supports indexing, and can be used as a context manager to ensure all proxies are closed when the batch is no longer needed.
Notes
Once closed, most operations raise
RuntimeError.Make sure to call
close()when done to release resources if not using a context manager.
Examples
Context manager usage:
>>> with pool.create_batch(3) as batch: ... for proxy in batch: ... print(proxy)
Manual close:
>>> batch = pool.create_batch(3) >>> for proxy in batch: ... print(proxy) >>> batch.close()
BaseProxyManager
- class auto_proxy_vpn.utils.base_proxy.BaseProxyManager[source]
-
- abstractmethod classmethod from_config(config=None, runtime_config: ManagerRuntimeConfig | None = None) BaseProxyManager[source]
- abstractmethod get_proxy(port: int = 0, size: Literal['small', 'medium', 'large'] = 'medium', region: str = '', auth: dict[Literal['user', 'password'], str] = {}, allowed_ips: str | list[str] = [], is_async: bool = False, retry: bool = True, proxy_name: str = '', on_exit: Literal['keep', 'destroy'] = 'destroy') T[source]
- abstractmethod get_proxy_by_name(name: str, is_async: bool = False, on_exit: Literal['destroy', 'keep'] = 'destroy') T[source]
- get_proxies(number: int, ports: list[int] | int = 0, sizes: list[Literal['small', 'medium', 'large']] | Literal['small', 'medium', 'large'] = 'medium', regions: list[str] | str = '', auths: list[dict[Literal['user', 'password'], str]] | dict[Literal['user', 'password'], str] = {}, allowed_ips: list[str] | str = [], is_async: bool = True, retry: bool = True, proxy_names: list[str] | str = '', on_exit: Literal['keep', 'destroy'] = 'destroy') ProxyBatch[T][source]
Gets multiple proxies at once. The parameters can be a single value or a list of values. If a single value is provided, it will be used for all proxies. If a list of values is provided, it must have the same length as the number of proxies to create.
- Parameters:
number (int) – Number of proxies to get.
regions (list[str] or str) – Region or list of regions to use.
allowed_ips (list[str] or str) – Allowed IP or list of allowed IPs to use in all proxies.
is_async (bool) – Whether the proxies should be async or not.
retry (bool) – Whether to retry if a proxy fails to start.
proxy_names (list[str] or str) – Proxy name or list of proxy names to use.
on_exit ({'keep', 'destroy'}) – Whether to keep or destroy the proxies when the program ends.
- Returns:
Batch of proxy instances. This is the return type for
get_proxies(...)in every provider manager implementation.- Return type:
- Raises:
ValueError – If the length of the ports, sizes, regions, auths or proxy_names lists is not equal to the number of proxies to create, or if any of the sizes or regions is invalid.
TypeError – If any of the auths is not a dict, or if allowed_ips is not a list of strings or a string.
KeyError – If any of the auth dicts does not have the keys ‘user’ and ‘password’.
- get_sizes_and_regions() dict[Literal['small', 'medium', 'large'], list[str] | list[tuple[str, list[str]]]][source]
Get all sizes and regions avaliable.
Exceptions
SSHClient
- class auto_proxy_vpn.utils.ssh_client.SSHClient(ip: str, user: str, strict: bool = False)[source]
Bases:
object- __init__(ip: str, user: str, strict: bool = False)[source]
Creates an SSH client to connect to a remote server and execute commands or download files. Checks the connection to the server before executing any command to ensure it is active.
- download_file(file: str, destination_file: str)[source]
Downloads a file from the remote server to the local machine.
- Parameters:
- Raises:
ConnectionError – If the connection to the server is not active.
FileNotFoundError – If the file does not exist on the remote server.
Utility Functions
- auto_proxy_vpn.utils.util.get_public_ip(timeout=2, proxy: dict[str, str] | None = None)[source]
Get the public IP address of the machine by querying multiple external services.