def send_request(url, proxy_host, timeout=3): headers = { 'Host': proxy_host, 'Referer': 'https://www.baidu.com/link?url=yZ8Z5H8yiKkLuxTC0mIBGIv3QFEvwmzu2gnzy-XU07URJkC4guyz6beWtZv8d4fh&wd=&eqid=8c6767b700042f580000000256e0d581', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 'Accept-Encoding': 'gzip' } try: req = urllib2.Request(url=url, headers=headers) resp = urllib2.urlopen(req, timeout=timeout) content = zhihu_util.get_content_from_resp(resp) return content except: print "send request error" return "FAIL"
def check_proxy(ip_proxy): proxy_handler = urllib2.ProxyHandler({ 'http': ip_proxy, 'https': ip_proxy }) opener = urllib2.build_opener(proxy_handler) # urllib2.install_opener(opener) headers = zhihu_util.get_headers() req = urllib2.Request( url="https://www.zhihu.com/question/40299633", headers=headers ) try: resp = opener.open(req, timeout=3) content = zhihu_util.get_content_from_resp(resp) print "check proxy %s:%s" % (ip_proxy, content != 'FAIL') return content != 'FAIL' except: print "check proxy %s:%s" % (ip_proxy, False) return False