-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.py
49 lines (43 loc) · 1.63 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import requests
import concurrent.futures
import asyncio
import time
results = []
def test_proxy(url, proxy, timeout):
http_proxy = "http://%s" %proxy
https_proxy = "https://%s" %proxy
ftp_proxy = "ftp://%s" %proxy
proxyDict = {
"http" : http_proxy,
"https" : https_proxy,
"ftp" : ftp_proxy
}
try:
print("Testing proxy %s" %proxy)
s = requests.Session()
start = time.time()
r = s.get(url, proxies=proxyDict, timeout=timeout)
end = time.time()
print("Proxy seems UP !\nAnswered in : %s" %(end-start))
if "detected" in r.text:
return {"success": "proxy answered in %s" %(end-start), "time": end-start, "detected": True, "proxy": proxy}
else:
return {"success": "proxy answered in %s" %(end-start), "time": end-start, "detected": False, "proxy": proxy}
except Exception as e:
print(e)
async def loop_test_proxies(proxies, timeout, threads):
global results
url = "https://monip.org"
with concurrent.futures.ThreadPoolExecutor(max_workers=threads) as executor:
loop = asyncio.get_event_loop()
futures = []
for proxy in proxies:
futures.append(loop.run_in_executor(executor, test_proxy, url, proxy, timeout))
for response in await asyncio.gather(*futures):
if response is not None:
results.append(response)
pass
def test_proxies(proxies, timeout, threads):
loop = asyncio.get_event_loop()
loop.run_until_complete(loop_test_proxies(proxies, timeout, threads))
return results