Created
April 28, 2019 12:48
-
-
Save maksimKorzh/d917d205bc7d6f994839d9f3d44db78b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import urllib | |
from collections import OrderedDict | |
proxies = {} | |
nextProxy = 0; | |
with open("workingproxy.txt") as f: | |
for line in f: | |
(key, val) = line.split('\n') | |
proxies[key] = 'http://' | |
OrderedDict(proxies) | |
proxyItems = list(proxies.items()) | |
def rotateProxy(): | |
global nextProxy | |
host = proxyItems[nextProxy][1] + proxyItems[nextProxy][0] | |
#print('Trying proxy:', host) | |
proxy = urllib.request.ProxyHandler({'http' : host}) | |
opener = urllib.request.build_opener(proxy) | |
urllib.request.install_opener(opener) | |
try: | |
reg = urllib.request.urlopen('http://www.google.com', timeout=1) | |
print(host, 'ok :', reg.getcode()) | |
nextProxy += 1 | |
if(reg.getcode() == 200): | |
return 200 | |
else: | |
return 0 | |
except: | |
nextProxy += 1 | |
return 0 | |
def getNextProxy(): | |
while (rotateProxy() != 200): | |
print('Bad proxy... Trying next...') | |
getNextProxy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment