2013-12-20 308 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'方法的調用。無法添加連接超時 - 除了在端點中,正如我在答案中所解釋的那樣。 –