Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External ip #11

Merged
merged 2 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions proxybroker/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Broker:
(optional) Flag indicating whether to check the SSL certificates.
Set to True to check ssl certifications
:param loop: (optional) asyncio compatible event loop
:param stop_broker_on_sigint: (optional) whether set SIGINT signal on broker object.
Useful for a thread other than main thread.

.. deprecated:: 0.2.0
Use :attr:`max_conn` and :attr:`max_tries` instead of
Expand All @@ -59,6 +61,7 @@ def __init__(
providers=None,
verify_ssl=False,
loop=None,
stop_broker_on_sigint=True,
**kwargs,
):
self._loop = loop or asyncio.get_event_loop()
Expand Down Expand Up @@ -101,13 +104,13 @@ def __init__(
p if isinstance(p, Provider) else Provider(p)
for p in (providers or PROVIDERS)
]

try:
self._loop.add_signal_handler(signal.SIGINT, self.stop)
# add_signal_handler() is not implemented on Win
# https://docs.python.org/3.5/library/asyncio-eventloops.html#windows
except NotImplementedError:
pass
if stop_broker_on_sigint:
try:
self._loop.add_signal_handler(signal.SIGINT, self.stop)
# add_signal_handler() is not implemented on Win
# https://docs.python.org/3.5/library/asyncio-eventloops.html#windows
except NotImplementedError:
pass

async def grab(self, *, countries=None, limit=0):
"""Gather proxies from the providers without checking.
Expand Down
11 changes: 8 additions & 3 deletions proxybroker/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Resolver:
'http://ipinfo.io/ip',
'http://ifconfig.io/ip',
]
# the list of resolvers will point a copy of original one
_temp_host = []

def __init__(self, timeout=5, loop=None):
self._timeout = timeout
Expand Down Expand Up @@ -85,13 +87,16 @@ def get_ip_info(ip):
return GeoData(code, name, region_code, region_name, city_name)

def _pop_random_ip_host(self):
host = random.choice(self._ip_hosts)
self._ip_hosts.remove(host)
host = random.choice(self._temp_host)
self._temp_host.remove(host)
return host

async def get_real_ext_ip(self):
"""Return real external IP address."""
while self._ip_hosts:
# make a copy of original one to temp one
# so original one will stay no change
self._temp_host = self._ip_hosts.copy()
while self._temp_host:
try:
timeout = aiohttp.ClientTimeout(total=self._timeout)
async with aiohttp.ClientSession(
Expand Down