dberube4

ProxyGenius

Aug 21st, 2012
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.27 KB | None | 0 0
  1. #Proxy Genius - by Dberube4
  2.  
  3. import urllib.request,tempfile,time
  4.  
  5. class proxyGenius():
  6.    
  7.     def __init__(self,dbgLvl = 0):
  8.         self.dbgLvl = dbgLvl
  9.         if 1: #Headers for the request EVERYTHING IS NEEDED
  10.             self.headers={}
  11.             self.headers['Host']='www.ip-adress.com'
  12.             self.headers['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0'
  13.             self.headers['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  14.             self.headers['Accept-Language']='fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
  15.             self.headers['Content-Type']='application/x-www-form-urlencoded;charset=utf-8'
  16.             self.headers['X-Moz']='prefetch'
  17.             self.headers['Accept-Charset']='ISO-8859-1,utf-8;q=0.7,*;q=0.7'
  18.             self.headers['Referer']='http://www.google.com'
  19.             self.headers['Cache-Control']='no-cache, no-cache'
  20.             self.headers['Pragma']='no-cache'
  21.         self.tmpDir = tempfile.gettempdir()+'/'
  22.         self.ip = self.getIp()
  23.         if self.dbgLvl == 1:
  24.             print('Proxy Genius Inited')
  25.    
  26.     def getIp(self):
  27.         url = urllib.request.urlopen('http://automation.whatismyip.com/n09230945.asp')
  28.         ip = str(url.read(),'utf8')
  29.         if self.dbgLvl == 1:
  30.             print('Public IP:',ip)
  31.         return ip
  32.    
  33.     def getLatestProxy(self):
  34.        
  35.         #send a request to the web page
  36.         req = urllib.request.Request('http://www.ip-adress.com/proxy_list/',headers=self.headers)
  37.         text = urllib.request.urlopen(req)
  38.         #save the webpage
  39.         name = self.tmpDir+'tmp.tmp'
  40.         tmp = open(name,'w').write(str(text.read(),'utf8'));
  41.        
  42.         #This get latest proxy (and related info) off the saved source.
  43.         a = open(name,'r');n = 0
  44.         while n != 250:
  45.             line = a.readline()
  46.             n+= 1
  47.         else:
  48.             proxy = a.readline().replace('<td>','');proxy = proxy.replace('</td>','');proxy = proxy.replace('\n','')
  49.             proxy = proxy.replace(' ','')
  50.         while n != 251:
  51.             line = a.readline()
  52.             n+= 1
  53.         else:
  54.             type = a.readline().replace('<td>','');type = type.replace('</td>','');type = type.replace('\n','')
  55.             type = type.replace('   ','')
  56.        
  57.         if self.dbgLvl == 1:
  58.             print('Proxy address: ',proxy,' Proxy type: ',type)
  59.         return([proxy,type])
  60.    
  61.     def listProxies(self,Filter = 'None',Max = 30):
  62.         #Possible Filter: 'None','Anonymous','Transparent','Elite',''
  63.         if Max > 30:
  64.             Max = 30
  65.  
  66.         temp = {'1':174,'2':210,'3':246,'4':282,'5':318,'6':354,'7':390,'8':426,'9':462,'10':498,'11':534,'12':570,'13':606,'14':642,'15':678,'16':714,'17':750,'18':786,'19':822,
  67.                 '20':858,'21':894,'22':930,'23':966,'24':1002,'25':1038,'26':1074,'27':1110,'28':1146,'29':1182,'30':1218}
  68.         proxDict = {}
  69.        
  70.         #send a request to the web page
  71.         req = urllib.request.Request('http://www.ip-adress.com/proxy_list/',headers=self.headers)
  72.         text = urllib.request.urlopen(req)
  73.         #save the webpage
  74.         name = self.tmpDir+'tmp.tmp'
  75.         tmp = open(name,'w').write(str(text.read(),'utf8'));
  76.        
  77.         #This get latest proxy (and related info) off the saved source.
  78.         a = open(name,'r');n = 0
  79.        
  80.         for i in range(1,Max+1):
  81.             number = temp[str(i)]
  82.             while n != number:
  83.                 line = a.readline()
  84.                 n+= 1
  85.             else:
  86.                 proxy = a.readline().replace('<td>','');proxy = proxy.replace('</td>','');proxy = proxy.replace('\n','')
  87.                 proxy = proxy.replace(' ','')
  88.             while n != number+1:
  89.                 line = a.readline()
  90.                 n+= 1
  91.             else:
  92.                 type = a.readline().replace('<td>','');type = type.replace('</td>','');type = type.replace('\n','')
  93.                 type = type.replace('   ','')
  94.            
  95.             if self.dbgLvl == 1:
  96.                 print('Proxy address: ',proxy,' Proxy type: ',type)
  97.                
  98.             if Filter == 'None':
  99.                 proxDict[str(i)] = [proxy,type]
  100.             elif Filter == 'Anonymous' and type == 'Anonymous':
  101.                 proxDict[str(i)] = [proxy,type]
  102.             elif Filter == 'Elite' and type == 'Elite':
  103.                 proxDict[str(i)] = [proxy,type]
  104.             elif Filter == 'Transparent' and type == 'Transparent':
  105.                 proxDict[str(i)] = [proxy,type]
  106.        
  107.         if len(proxDict) == 0:
  108.             print('No proxy found.If you are using a filter,set the max to 30')
  109.        
  110.         return proxDict
  111.    
  112.     def testProxy(self,proxy):
  113.         if self.dbgLvl == 1:
  114.             print('Testing',proxy,'...')
  115.         try:
  116.             req = urllib.request.Request('http://automation.whatismyip.com/n09230945.asp')
  117.             req.set_proxy(proxy,'http')
  118.             text = urllib.request.urlopen(req)
  119.             ip = str(text.read(),'utf8')
  120.             if ip == self.ip:
  121.                 if self.dbgLvl == 1:print('Proxy is not working!')
  122.                 return False
  123.             else:
  124.                 if self.dbgLvl == 1:print('Proxy is working!')
  125.                 return True
  126.         except:
  127.             return False
  128.        
  129.            
  130. if __name__ == '__main__':
  131.     print('''Proxy Genius self execution.\nCommands:\n1: Print latest proxy information.\n2: Print a list of proxies\n3: Get your public ip\n4: Test a proxy\n''')
  132.     genius = proxyGenius(0)
  133.     while 1:
  134.         Input = str(input('Command Number: '))
  135.         try:
  136.             if Input == '1':
  137.                 print(genius.getLatestProxy())
  138.             elif Input == '2':
  139.                 Filter = input('Select Filter (None,Anonynous,Elite,Transparent): ')
  140.                 num = int(input('Select number of proxies to get(30 = Max): '))
  141.                 print(genius.listProxies(Filter,num))
  142.             elif Input == '3':
  143.                 print(genius.getIp())
  144.             elif Input == '4':
  145.                 proxy = input('Enter proxy([Domain]:[Port]): ')
  146.                 print(genius.testProxy(proxy))
  147.             else:
  148.                 print('This is not a valid input')
  149.         except:
  150.             print('Something went wrong.')
Advertisement
Add Comment
Please, Sign In to add comment