简体   繁体   English

如何设置ProxyAgent的超时时间?

[英]How to set timeout for ProxyAgent?

The client.Agent class has a connection timeout argument: client.Agent类具有连接超时参数:

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

How can this timeout be set when using client.ProxyAgent ? 使用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)

The TCP4ClientEndpoint you pass to ProxyAgent can be initialized with a timeout. TCP4ClientEndpoint传递给ProxyAgent可以用超时而被初始化。

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)

This is supposing you want to set the timeout for connecting to the proxy. 假设您要设置连接代理的超时时间。 If you wanted to set the timeout used by the proxy to connect to the upstream HTTP server, you can't control this. 如果要设置代理连接到上游HTTP服务器所用的超时,则无法控制。

It looks like client.ProxyAgent doesn't have a connectTimeout property: 看起来像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 inherits from the same class Agent does ( _AgentBase ) and not from Agent itself. ProxyAgent继承自Agent的同一类( _AgentBase ),而不继承自Agent本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM