# -*- coding: cp936 -*- import urllib2,re,thread,time import socket socket.setdefaulttimeout(10) def proxycheckone(proxy = '24.48.219.49:8080'): url='http://www.sohu.com' proxy_url = 'http://'+proxy proxy_support = urllib2.ProxyHandler({'http': proxy_url}) httpHandler = urllib2.HTTPHandler(debuglevel=1) opener = urllib2.build_opener(proxy_support, httpHandler) r=urllib2.Request(url) r.add_header("Accept-Language","zh-cn") #加入头信息,这样可以避免403错误 r.add_header("Content-Type","text/html; charset=gb2312") r.add_header("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)") trycount=1 while trycount<=2: try: T0=time.time() f=opener.open(r) print time.time()-T0 data=f.read() if 'sohu' in data: T=time.time()-T0 break else:return [] except: time.sleep(3) trycount=trycount+1 if trycount>2: return [] else: return proxy+'$'+str(trycount)+'#'+str(T) proxycheckone()