如何为ProxyAgent设置超时?

时间:2013-12-20 22:03:34

标签: python twisted

client.Agent类有一个连接超时参数:

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 个答案:

答案 0 :(得分:2)

您传递给TCP4ClientEndpoint的{​​{1}}可以使用超时进行初始化。

ProxyAgent

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

答案 1 :(得分:0)

看起来client.ProxyAgent没有connectTimeout属性:

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继承自同一个班级Agent_AgentBase)而不是Agent本身。