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

Fix Windows NotImplementedError / Hanging of program #147

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 7 additions & 2 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Find and show 10 working HTTP(S) proxies."""

import sys
import asyncio

from proxybroker import Broker
Expand All @@ -12,10 +13,14 @@ async def show(proxies):
break
print('Found proxy: %s' % proxy)

if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

proxies = asyncio.Queue()
broker = Broker(proxies)
broker = Broker(proxies, loop=loop)
tasks = asyncio.gather(broker.find(types=['HTTP', 'HTTPS'], limit=10), show(proxies))

loop = asyncio.get_event_loop()
loop.run_until_complete(tasks)
8 changes: 6 additions & 2 deletions examples/find_and_save.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Find 10 working HTTP(S) proxies and save them to a file."""

import sys
import asyncio

from proxybroker import Broker

if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def save(proxies, filename):
"""Save proxies to a file."""
Expand All @@ -18,13 +21,14 @@ async def save(proxies, filename):


def main():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
proxies = asyncio.Queue()
broker = Broker(proxies)
broker = Broker(proxies, loop=loop)
tasks = asyncio.gather(
broker.find(types=['HTTP', 'HTTPS'], limit=10),
save(proxies, filename='proxies.txt'),
)
loop = asyncio.get_event_loop()
loop.run_until_complete(tasks)


Expand Down
6 changes: 5 additions & 1 deletion examples/find_and_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Perhaps it will be much useful and friendlier.
"""

import sys
import asyncio
from urllib.parse import urlparse

Expand All @@ -12,6 +13,8 @@
from proxybroker import Broker, ProxyPool
from proxybroker.errors import NoProxyError

if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def fetch(url, proxy_pool, timeout, loop):
resp, proxy = None, None
Expand Down Expand Up @@ -47,7 +50,8 @@ async def get_pages(urls, proxy_pool, timeout=10, loop=None):


def main():
loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

proxies = asyncio.Queue()
proxy_pool = ProxyPool(proxies)
Expand Down
8 changes: 7 additions & 1 deletion examples/only_grab.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Gather proxies from the providers without
checking and save them to a file."""

import sys
import asyncio

from proxybroker import Broker

if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def save(proxies, filename):
"""Save proxies to a file."""
Expand All @@ -17,8 +20,11 @@ async def save(proxies, filename):


def main():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

proxies = asyncio.Queue()
broker = Broker(proxies)
broker = Broker(proxies, loop=loop)
tasks = asyncio.gather(
broker.grab(countries=['US', 'GB'], limit=10),
save(proxies, filename='proxies.txt'),
Expand Down
6 changes: 5 additions & 1 deletion examples/proxy_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Run a local proxy server that distributes
incoming requests to external proxies."""

import sys
import asyncio

import aiohttp

from proxybroker import Broker

if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def fetch(url, proxy_url):
resp = None
Expand Down Expand Up @@ -35,7 +38,8 @@ async def get_pages(urls, proxy_url):
def main():
host, port = '127.0.0.1', 8888 # by default

loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

types = [('HTTP', 'High'), 'HTTPS', 'CONNECT:80']
codes = [200, 301, 302]
Expand Down
8 changes: 6 additions & 2 deletions examples/proxy_smtp_port.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Find 10 working proxies supporting CONNECT method
to 25 port (SMTP) and save them to a file."""

import sys
import asyncio

from proxybroker import Broker

if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def save(proxies, filename):
"""Save proxies to a file."""
Expand All @@ -17,8 +20,10 @@ async def save(proxies, filename):


def main():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
proxies = asyncio.Queue()
broker = Broker(proxies, judges=['smtp://smtp.gmail.com'], max_tries=1)
broker = Broker(proxies, judges=['smtp://smtp.gmail.com'], max_tries=1, loop=loop)

# Check proxy in spam databases (DNSBL). By default is disabled.
# more databases: http://www.dnsbl.info/dnsbl-database-check.php
Expand All @@ -35,7 +40,6 @@ def main():
broker.find(types=['CONNECT:25'], dnsbl=dnsbl, limit=10),
save(proxies, filename='proxies.txt'),
)
loop = asyncio.get_event_loop()
loop.run_until_complete(tasks)


Expand Down
6 changes: 5 additions & 1 deletion examples/use_existing_proxy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Run a local proxy server that distributes
incoming requests to external proxies."""

import sys
import asyncio

import aiohttp

if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def fetch(url, proxy_url):
resp = None
Expand Down Expand Up @@ -33,7 +36,8 @@ async def get_pages(urls, proxy_url):
def main():
host, port = '127.0.0.1', 8888 # by default

loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# types = [('HTTP', 'High'), 'HTTPS', 'CONNECT:80']
# codes = [200, 301, 302]
Expand Down
2 changes: 1 addition & 1 deletion proxybroker/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(
stop_broker_on_sigint=True,
**kwargs,
):
self._loop = loop or asyncio.get_event_loop_policy().get_event_loop()
self._loop = loop
ziloka marked this conversation as resolved.
Show resolved Hide resolved
self._proxies = queue or asyncio.Queue()
self._resolver = Resolver(loop=self._loop)
self._timeout = timeout
Expand Down
2 changes: 1 addition & 1 deletion proxybroker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(
self._strict = strict
self._dnsbl = dnsbl or []
self._types = types or {}
self._loop = loop or asyncio.get_event_loop()
self._loop = loop
self._resolver = Resolver(loop=self._loop)

self._req_http_proto = not types or bool(
Expand Down
3 changes: 2 additions & 1 deletion proxybroker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ def cli(args=sys.argv[1:]):
ns.types.remove('HTTP')
ns.types.append(('HTTP', ns.anon_lvl))

loop = asyncio.get_event_loop_policy().get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
proxies = asyncio.Queue()
broker = Broker(
proxies,
Expand Down
2 changes: 1 addition & 1 deletion proxybroker/judge.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, url, timeout=8, verify_ssl=False, loop=None):
self.marks = {'via': 0, 'proxy': 0}
self.timeout = timeout
self.verify_ssl = verify_ssl
self._loop = loop or asyncio.get_event_loop()
self._loop = loop
self._resolver = Resolver(loop=self._loop)

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion proxybroker/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(
self._proxies = set()
# concurrent connections on the current provider
self._sem_provider = asyncio.Semaphore(max_conn)
self._loop = loop or asyncio.get_event_loop()
self._loop = loop
ziloka marked this conversation as resolved.
Show resolved Hide resolved

@property
def proxies(self):
Expand Down
2 changes: 1 addition & 1 deletion proxybroker/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Resolver:

def __init__(self, timeout=5, loop=None):
self._timeout = timeout
self._loop = loop or asyncio.get_event_loop()
self._loop = loop
self._resolver = aiodns.DNSResolver(loop=self._loop)

@staticmethod
Expand Down
5 changes: 2 additions & 3 deletions proxybroker/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(
):
self.host = host
self.port = int(port)
self._loop = loop or asyncio.get_event_loop()
self._loop = loop
self._timeout = timeout
self._max_tries = max_tries
self._backlog = backlog
Expand All @@ -151,8 +151,7 @@ def start(self):
self._accept,
host=self.host,
port=self.port,
backlog=self._backlog,
loop=self._loop,
backlog=self._backlog
)
self._server = self._loop.run_until_complete(srv)

Expand Down
Loading