Skip to content

Instantly share code, notes, and snippets.

@shuivin
Created Nov 14, 2012
Embed
What would you like to do?
Python:IPProxy finder
#-*- encoding=cp936 -*-
import urllib,re,subprocess
import pdb,os
class IPSpider():
def __init__(self):
self.url = "http://www.cnproxy.com/proxyedu1.html"
self.urllist = []
self.iplist = []
self.shortiplist = []
self.ipdict = {}
self.portdict = {
'z':3, 'm':4, 'k':2, 'l':9, 'd':0,
'b':5, 'i':7, 'w':6, 'r':8, 'c':1}
def getContent(self):
#get the webpage content
#Todo has misstick to has chinese in content
#Todo ecception dection not do
weburl = self.url
webpage = urllib.urlopen(weburl).read()
self.saveContent(webpage)
self.getIPdata(webpage)
print self.iplist
def saveContent(self, webstr):
webfile = open(".\\webpage.back",'w')
webfile.write(webstr)
webfile.close()
print "webpage Cache BackUP OK@@@"
def getbackupContent(self):
webpage = open('D:\\temp\\test.log', 'r')
webpagebuf = webpage.read()
webpage.close()
#pdb.set_trace()
self.getIPdata(webpagebuf)
#print self.iplist
def getIPdata(self, webpage):
#get the IPdata for the webpage
#Todo has not get the region becouse of miss code of Chinese
reword = re.compile(r'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*"((\+(r|d|z|m|k|l|b|i|w|r|c)){2,})')
for tmp in reword.finditer(webpage):
tmp1 =tmp.groups()
ip = self.getip(tmp1)
port = self.getport(tmp1)
self.shortiplist.append(ip)
self.iplist.append( ip+ ":"+ port )
self.ipdict[ip] = port
def getip(self, tmp):
#pdb.set_trace()
ip = '.'.join( tmp[0:4] )
#ip = eval(ip)
return ip
def getport(self, tmp):
num = ''
for key in tmp[4]:
if self.portdict.has_key(key):
num += str(self.portdict[key])
#num = eval(num)
return num
def saveIPlist(self, filename = '.\\IPdata.txt'):
#save the ip list
#getContent()
self.getbackupContent()
IPdata = open(filename, 'w+')
for ip in self.iplist:
IPdata.write(ip+"\n")
def saveIPdata(self,timelist, filename = '.\\IPdata.txt'):
#save the ip list
#getContent()
#self.getbackupContent()
IPdata = open(filename, 'w+')
for ip in timelist:
#pdb.set_trace()
IPdata.write(ip[0]+":"+str(ip[1])+"\n")
def ping_test(self, myiplist):
from operator import itemgetter
testip = ''
timedict = {}
for testip in myiplist:
p = os.popen('ping -n 1 '+ testip, 'r')
#pdb.set_trace()
out = p.read()
regex = re.compile(r'(\d+)ms\n')
tmplist = regex.findall(out)
#print out
if [] != tmplist:
pingtime = eval(regex.findall(out)[0])
#pingtime = regex.findall(out)[2]
print 'IP : -- ' + testip + ' --respond time is :' +str(pingtime)
timedict[testip] = pingtime
timelist = sorted(timedict.iteritems(), key=itemgetter(1), reverse=False)
ss = "\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n"
print ss+"The best Proxy IP is :\n"+ str(timelist[0][0])+ ':'+self.ipdict[timelist[0][0]]+ ss + '\n\n'
self.saveIPdata(timelist)
return timedict
if __name__ == "__main__":
ips = IPSpider()
#cc = ips.getip(iplist)
#print cc
#pdb.set_trace()
#ips.getContent()
ips.getbackupContent()
#ips.saveIPdata()
ips.ping_test(ips.shortiplist)
print 'end#######################'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment