我正在使用一些 urllib2.HTTPHandler 子类作为socksipy 项目的代理。
一切都很好,除了当他们点击一个挂起的 URL 时。全局和通过 OpenDirector.open() 方法设置超时不会做任何事情。
以下是 urllib2.HTTPHandlers:
# get the socksipy project code
import socks
class SocksiPyConnection(httplib.HTTPConnection):
def __init__(self, proxytype, proxyaddr, proxyport = None, rdns = False, 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)
我尝试将超时全局设置为socket.setdefaulttimeout(30),但没有成功。当我在上面实例化SocksiPyConnection时,我也尝试过设置超时 。最后,我尝试使用OpenDirector.open方法设置超时,因为 API 说它需要超时而没有成功。
挂起的测试代码:
import sys
# import socksipy base code
sys.path.append( "/parent/path/to/socks.py" )
import socks
import urllib2
import socket
socket.setdefaulttimeout(30)
proxyhost = "responder.w2"
proxyport = 1050
sys.path.append( "/home/gcorradini" )
from sock_classes import SocksiPyHandler
opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, proxyhost, int(proxyport)) )
resp = opener.open("http://erma.orr.noaa.gov/cgi-bin/mapserver/charts?version=1.1.1&service=wms&request=GetCapabilities", timeout=30.0)
# i just hang here forever