better api

master
Felix Pankratz 4 years ago
parent 54b6ca668e
commit b84918933b

@ -5,22 +5,31 @@ from urllib import request, error
import socket import socket
import sys import sys
target = "http://theresno.cloud/ip" target = "http://theresno.cloud/ip"
prx_api = "http://pubproxy.com/api/proxy?limit=10&http=true&format=txt" prx_api = "https://www.proxyscan.io/api/proxy?last_check=9800&limit=10&type=http" #"http://pubproxy.com/api/proxy?limit=10&http=true&format=txt"
def get_list_from_api(): def get_list_from_api():
import json
print('Requesting proxy list... ', end='') print('Requesting proxy list... ', end='')
result = request.urlopen(prx_api) result = request.urlopen(prx_api)
prx_list = result.read().decode().split('\n') # json magic starts here
print('Got {} proxies.'.format(len(prx_list))) content = result.read().decode()
json_data = json.loads(content)
prx_list = ["".join(json_data[i]["Ip"] + ':' + str(json_data[i]["Port"])) for i in range(len(json_data))]
#= result.read().decode().split('\n')
print('got {} proxies.'.format(len(prx_list)))
return prx_list return prx_list
def test_proxy(prx): def test_proxy(prx):
try:
from http.client import RemoteDisconnected
except ImportError:
from httplib import BadStatusLine as RemoteDisconnected
srv_string = "Testing proxy " + prx + "... " srv_string = "Testing proxy " + prx + "... "
print(srv_string, end='\r') print(srv_string, end='\r')
proxy_handler = request.ProxyHandler({'https': 'http://' + prx + '/', 'http': 'http://' + prx + '/' }) proxy_handler = request.ProxyHandler({'https': 'http://' + prx + '/', 'http': 'http://' + prx + '/' })
opener = request.build_opener(proxy_handler) opener = request.build_opener(proxy_handler)
try: try:
result = opener.open(target, timeout=5) result = opener.open(target, timeout=10)
content = result.read().decode() content = result.read().decode()
print(srv_string + 'ok. \u2705') print(srv_string + 'ok. \u2705')
return True return True
@ -30,9 +39,9 @@ def test_proxy(prx):
except error.URLError: except error.URLError:
print(srv_string + 'down! \u2935') print(srv_string + 'down! \u2935')
return False return False
#except error.RemoteDisconnected: except RemoteDisconnected:
# print(srv_string + 'connection fail!') print(srv_string + 'connection fail!')
# return False return False
except socket.timeout: except socket.timeout:
print(srv_string + 'timeout! \u23f3') print(srv_string + 'timeout! \u23f3')
return False return False

Loading…
Cancel
Save