proxy_random package

Submodules

random_proxy module

contains the main class to manage providers and proxies.

class proxy_random.random_proxy.RandomProxy(verify: bool = False, test_url: Optional[str] = None, timeout: Optional[int] = None, use_defaults: bool = True, proxy: Optional[str] = None)[source]

Bases: object

main class used to manage providers and proxies

add_provider(provider: proxy_random.provider.Provider) None[source]

add a provider to the list of providers

Parameters

provider (Provider) – a provider should be an instance of Provider class

Raises

TypeError – raises TypeError if provider is not an instance of Provider class

extract_proxies() proxy_random.query.ProxyQuery[source]

extracts the proxies from providers

Returns

a ProxyQuery object containing the proxies

Return type

ProxyQuery

proxy module

contains the Proxy and BaseProxy class which contains the information about proxies.

class proxy_random.proxy.BaseProxy(ip: Optional[str] = None, port: Optional[int] = None, country_code: Optional[str] = None, country: Optional[str] = None, anonymity: Optional[str] = None, google: Optional[bool] = None, https: Optional[bool] = None, last_checked: Optional[str] = None, **kwargs)[source]

Bases: object

The base proxy class

property url: str

port

Returns

proxy url

Return type

str

Type

the proxy url in format of ip

class proxy_random.proxy.Proxy(*args, **kwargs)[source]

Bases: proxy_random.proxy.BaseProxy

the main proxy object that contains all the information about the proxy use this class instead of BaseProxy if you want to use the proxy object. it’s most likely that you won’t use this class directly if you use the default providers.

query module

contains the class used to query fetched proxies

class proxy_random.query.ProxyQuery(proxy_list: List[proxy_random.proxy.Proxy])[source]

Bases: object

ProxyQuery class used to work with fetched proxies.

asc() proxy_random.query.ProxyQuery[source]

use after order_by() to order in descending order. (doesn’t do anything in reality)

Returns

returns the ProxyQuery

Return type

ProxyQuery

check_health(test_url=None, timeout=None) proxy_random.query.ProxyQuery[source]

Check health of proxies.

Parameters
  • test_url (str, optional) – test url used to check health of proxies, if not provided default url will be used, defaults to None

  • timeout (int, optional) – timeout used in the test request, if not provided 5 seconds will be used, defaults to None

Returns

The proxy query with updated proxies

Return type

ProxyQuery

desc() proxy_random.query.ProxyQuery[source]

reverses the proxies order. use after order_by() to order in descending order.

Returns

returns a new ProxyQuery with the proxies in reversed order

Return type

ProxyQuery

filter(ip: Optional[str] = None, port: Optional[Union[int, List[int]]] = None, country_code: Optional[Union[str, List[str]]] = None, country: Optional[Union[str, List[str]]] = None, anonymity: Optional[Union[str, List[str]]] = None, google: Optional[bool] = None, https: Optional[bool] = None, verified: Optional[bool] = None, working: Optional[bool] = None, last_checked: Optional[Union[str, List[str]]] = None, custom_filters: Optional[Union[Callable, List[Callable]]] = None) proxy_random.query.ProxyQuery[source]

method used to filter the proxies.

Parameters
  • ip (str, optional) – filter the IP address (useless), defaults to None

  • country_code (Union[str, list[str]], optional) – filter based on the country code can be a country code or list of them, defaults to None

  • country (Union[str, list[str]], optional) – filter based on country can be a country or list of them, defaults to None

  • anonymity (Union[str, list[str]], optional) – filter based on anonymity, can be a list or string, defaults to None

  • google (bool, optional) – filter based on google, defaults to None

  • https (bool, optional) – filter based on whether it’s https or not, defaults to None

  • verified (bool, optional) – filter based on if it’s tested or not (if it’s tested it doesn’t mean it’s working for that purpose use working instead), defaults to None

  • working (bool, optional) – filter based on it’s working or not(proxies should be verified use check_health before filtering based on this field), defaults to None

  • last_checked (Union[ str, list[str] ], optional) – filter based on last checked time (don’t use), defaults to None

Returns

returns a new ProxyQuery with filtered proxies

Return type

ProxyQuery

first() Optional[proxy_random.proxy.Proxy][source]

returns the first proxy from the ProxyQuery.

Returns

first proxy in the ProxyQuery

Return type

Union[Proxy, None]

last() Optional[proxy_random.proxy.Proxy][source]

returns the last proxy from the ProxyQuery.

Returns

last proxy in the ProxyQuery

Return type

Union[Proxy, None]

limit(limit: int) proxy_random.query.ProxyQuery[source]

returns a new ProxyQuery with the first limit proxies. :param limit: the number of proxies to return :type limit: int :return: the first limit proxies :rtype: ProxyQuery

order_by(attribute: str) proxy_random.query.ProxyQuery[source]

order the proxies by the given attribute in ascending order. use desc() after order_by() to order in descending order.

Parameters

attribute (str) – attribute to order the proxies by

Raises

AttributeError – raises AttributeError if the attribute doesn’t exist

Returns

returns a new ProxyQuery with proxies in ordered

Return type

ProxyQuery

random() proxy_random.proxy.Proxy[source]

returns a random proxy from the ProxyQuery.

Returns

a random proxy

Return type

Proxy

union(other: proxy_random.query.ProxyQuery) proxy_random.query.ProxyQuery[source]

returns a new ProxyQuery with the union of the proxies in the ProxyQuery and the other ProxyQuery.

Parameters

other (ProxyQuery) – the other ProxyQuery

Returns

the union of the proxies in the ProxyQuery and the other ProxyQuery

Return type

ProxyQuery

provider module

contains the provider class which is used to register a provider and parse the response.

class proxy_random.provider.Provider(url: str, extractor: Optional[Callable] = None)[source]

Bases: object

The proxy provider class

async extract(session: aiohttp.client.ClientSession) None[source]

the method used to extract proxies from the provider. shouldn’t be used directly, use the extract_proxies method in RandomProxy class instead.

Parameters

session (ClientSession) – session used to fetch the url

Raises

ValueError – raises ValueError if the extractor function is not provided.

get_proxy_query() proxy_random.query.ProxyQuery[source]

returns the proxy query object.

Returns

the proxy query object

Return type

ProxyQuery

set_extractor(extractor: Callable) None[source]

used to set the extractor function if not provided in the constructor.

Parameters

extractor (Callable) – the extractor function.

Raises

TypeError – raises TypeError if extractor is not a function

Module contents

proxy_random - A simple package to get random proxy from online proxylist sites.