Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # import thư viện
- import socks
- import httplib
- import urllib2
- import sys
- import re
- import socket
- import time
- import threading
- from configobj import ConfigObj
- # Tên File config
- filename = 'X-Proxy.config'
- config = ConfigObj(filename)
- # Load config từ file config
- try:
- SetTimeOut = config['SetTimeOut'] # set timeout Ex : True/False
- TimeOut = config['TimeOut'] # set timeout Ex : 15
- SaveOutput = config['SaveOutput'] # có lưu file output không Ex : True/False
- OutPutFile = config['OutPutFile'] # tên file output
- DebugMode = config['DebugMode'] # hiện thị thông tin debug
- ProxyFile = config['ProxyFile'] # tên file proxy sẽ load vào
- Url_TestProxy = config['Url_TestProxy'] # url test proxy
- User_Agent = config['User_Agent']
- #print 'Loaded Config File : ' + filename
- if DebugMode =='True':
- print 'Config Info :\n'
- print "SetTimeOut : " + SetTimeOut
- print "TimeOut : " + TimeOut
- print "SaveOutput : " + SaveOutput
- print "OutPutFile : " + OutPutFile
- print "DebugMode : " + DebugMode
- print "ProxyFile : " + ProxyFile
- print "Url_TestProxy : " + Url_TestProxy
- print 'User_Agent : ' + User_Agent + '\n'
- except:
- print 'Load File : ' + filename + ' error'
- print 'Exit'
- sys.exit(1)
- class SocksiPyConnection(httplib.HTTPConnection):
- def __init__(self, proxytype, proxyaddr, proxyport = None, rdns = True, username = None, password = None, *args, **kwargs):
- self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
- httplib.HTTPConnection.__init__(self, *args, **kwargs)
- def connect(self):
- self.sock = socks.socksocket()
- self.sock.setproxy(*self.proxyargs)
- if isinstance(self.timeout, float):
- self.sock.settimeout(self.timeout)
- self.sock.connect((self.host, self.port))
- class SocksiPyHandler(urllib2.HTTPHandler):
- def __init__(self, *args, **kwargs):
- self.args = args
- self.kw = kwargs
- urllib2.HTTPHandler.__init__(self)
- def http_open(self, req):
- def build(host, port=None, strict=None, timeout=0):
- conn = SocksiPyConnection(*self.args, host=host, port=port, strict=strict, timeout=timeout, **self.kw)
- return conn
- return self.do_open(build, req)
- def GetProxyList():
- try:
- print 'Load Proxy File : ' + ProxyFile
- proxy_list = []
- with open(ProxyFile) as f:
- for line in f.readlines():
- proxy = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\:(?:[\d]{1,5})',line)
- if proxy:
- if any(proxy[0] in item for item in proxy_list)==False:
- proxy_list.append(proxy[0])
- if DebugMode =='True':
- print proxy[0] + ' append To Proxy List'
- else:
- continue
- lenght = str(len(proxy_list))
- if lenght<1:
- print 'Proxy not found !'
- sys.exit(1)
- elif lenght==1:
- print 'Proxy: 1'
- else:
- print 'Proxies: ' + lenght
- return proxy_list
- except:
- print 'Load Proxy File ' + ProxyFile + ' Error'
- sys.exit(1)
- def CheckProxy(proxy):
- try:
- s = socks.socksocket()
- s.setproxy(socks.PROXY_TYPE_SOCKS5, proxy.split(':')[0], int(proxy.split(':')[1]))
- DATA = ('GET / HTTP/1.1\r\n' +
- 'Host: %s\r\n' +
- 'User-Agent: %s\r\n' +
- 'Connection: close\r\n' +
- '\r\n') % (Url_TestProxy,User_Agent)
- if DebugMode =='True': print 'Headers : ' + DATA
- s.connect((Url_TestProxy, 80))
- s.send(DATA)
- buf = s.recv(8)
- html = ''
- while len(buf):
- html += buf
- buf = s.recv(16)
- if DebugMode =='True': print 'Recv : ' + html
- if html.find('HTTP/1.1')>=0:
- s.close()
- return 1
- break
- s.close()
- except:
- return 0
- def main(proxy):
- print 'Check Proxy ' + proxy + '\n'
- if CheckProxy(proxy) == 1:
- if SaveOutput == 'True':
- print 'Proxy ' + proxy + ' Live'
- open(OutPutFile, 'a').write('%s\n'%proxy)
- if DebugMode =='True': print proxy + ' writed to ' + OutPutFile
- sys.exit(1)
- else:
- print 'Proxy ' + proxy + ' Die'
- sys.exit(1)
- if __name__ == '__main__':
- start_time = time.clock()
- NumberThread = 5
- i = 0
- j = 0
- Start_NumberThread = 0
- End_NumberThread = NumberThread
- proxy_list = GetProxyList()
- if SetTimeOut=='True':
- socket.setdefaulttimeout(float(TimeOut))
- if DebugMode =='True': print 'Used TimeOut ' + str(TimeOut)
- while i < len(proxy_list):
- if NumberThread < len(proxy_list):
- for j in (Start_NumberThread, End_NumberThread):
- proxy = proxy_list[j]
- threads = []
- thread = threading.Thread(target = main,args = (proxy,))
- thread.start()
- threads.append(thread)
- for thread in threads:
- thread.join()
- else:
- print 'NumberThread = ' + str(NumberThread) + ' > ' + 'Total Proxy = ' + str(len(proxy_list))
- i = i + NumberThread
- j = Start_NumberThread
- Start_NumberThread = i
- End_NumberThread = i + NumberThread - 1
- end_time = time.clock()
- run_time = end_time - start_time
- print 'Time Check : %.2gs' % (run_time)
- open(OutPutFile, 'a').write('Time Check : %.2gs\nTools by parkdream1\n'%(run_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement