
import requests
import re
import socks
import socket
import threading

lock = threading.Lock()
url = "http://www.youdaili.net/Daili/Socks/4189.html"
target = "http://www.sina.com.cn"
html_doc = requests.get(url).content
ipReg = re.compile(r"(\d+\.\d+\.\d+\.\d+):(\d+)@(\w+)#")
proxies = ipReg.findall(html_doc)
f = open('proxies.txt','w')
def testProxies(proxy):
	try:
		if proxy[2] == "SOCKS4":
			socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,proxy[0],int(proxy[1]))
		elif proxy[2] == "SOCKS5":
			socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,proxy[0],int(proxy[1]))
		else:
			return
		socket.socket = socks.socksocket
		result = requests.get(target,timeout=4)
		if result.status_code== 200:
			lock.acquire()
			f.write(proxy[2].lower()+'  '+proxy[0]+' '+proxy[1]+"\n")
			print "available proxy:"+proxy[0]
			lock.release()
	except Exception,e:
		print "failed to connecting:"+proxy[0]

threads = []
for proxy in proxies:
	t = threading.Thread(target=testProxies,args=(proxy,))
	threads.append(t)
	t.start()
for t in threads:
	t.join()
f.close()
