proxy_handler urllib2 ProxyHandler http http item opener urllib2 build

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
proxy_handler = urllib2.ProxyHandler({'http': 'http://' + item})
opener = urllib2.build_opener(proxy_handler)
post_key = str(time.time()).replace('.', '-')
post_value = str(time.time()).replace('.', '-')
post_data = urllib.urlencode({post_key: post_value})
start_time = time.time()
try:
data = opener.open(settings.PROXY_CHECK_URL, post_data).read(3000)
except Exception, ex:
try:
reason = unicode(ex).encode('utf-8')
except Exception:
reason = 'Could not parse error'
return {'type': PROXY_BAD, 'proxy': item, 'reason': reason}
else:
timeout = '%.2f' % (time.time() - start_time)