2013-12-20 312 views
1

client.Agent类有一个连接超时参数:如何设置ProxyAgent的超时时间?

agent = client.Agent(reactor, connectTimeout=timeout, pool=pool) 

这怎么超时使用client.ProxyAgent时设置?

auth = base64.b64encode("%s:%s" % (username, password)) 
headers['Proxy-Authorization'] = ["Basic " + auth.strip()] 
endpoint = endpoints.TCP4ClientEndpoint(reactor, host, port) 
agent = client.ProxyAgent(endpoint, reactor=reactor, pool=pool) 

回答

2

您传递给ProxyAgentTCP4ClientEndpoint可以用超时初始化。

auth = base64.b64encode("%s:%s" % (username, password)) 
headers['Proxy-Authorization'] = ["Basic " + auth.strip()] 
endpoint = endpoints.TCP4ClientEndpoint(reactor, host, port, timeout=yourTimeout) 
agent = client.ProxyAgent(endpoint, reactor=reactor, pool=pool) 

这是假设您想要设置连接到代理的超时。如果您想设置代理使用的连接到上游HTTP服务器的超时时间,则无法控制此设置。

+0

是试图为整个请求设置超时,包括上游HTTP服务器。我发现默认超时时间太短。 – hoju

+0

“Agent”的'connectTimeout'参数不是整个请求超时。这只是TCP连接尝试的超时。我的答案中的代码实现完全相同的事情。如果你想要的不是TCP连接尝试超时,那么我认为你的问题是误导。 –

+0

谢谢,这是有道理的。我设置了此连接超时,并创建了一个包含延迟超时的包装类,以便在往返时间过长时取消请求 – hoju

0

它看起来像client.ProxyAgent没有connectTimeout属性:从Agent本身同一类Agent做(_AgentBase

class ProxyAgent(_AgentBase): 
    """ 
    An HTTP agent able to cross HTTP proxies. 

    @ivar _proxyEndpoint: The endpoint used to connect to the proxy. 

    @since: 11.1 
    """ 

    def __init__(self, endpoint, reactor=None, pool=None): 
     if reactor is None: 
      from twisted.internet import reactor 
     _AgentBase.__init__(self, reactor, pool) 
     self._proxyEndpoint = endpoint 


    def request(self, method, uri, headers=None, bodyProducer=None): 
     """ 
     Issue a new request via the configured proxy. 
     """ 
     # Cache *all* connections under the same key, since we are only 
     # connecting to a single destination, the proxy: 
     key = ("http-proxy", self._proxyEndpoint) 

     # To support proxying HTTPS via CONNECT, we will use key 
     # ("http-proxy-CONNECT", scheme, host, port), and an endpoint that 
     # wraps _proxyEndpoint with an additional callback to do the CONNECT. 
     return self._requestWithEndpoint(key, self._proxyEndpoint, method, 
             _URI.fromBytes(uri), headers, 
             bodyProducer, uri) 

ProxyAgent继承,而不是。

+0

这太可惜了,所以似乎需要创建一个新类来支持这个 – hoju

+0

新的类没有帮助。注意'ProxyAgent'中没有任何'reactor.connectXXX'方法的调用。无法添加连接超时 - 除了在端点中,正如我在答案中所解释的那样。 –