@implementer(IAgent) class Agent(_AgentBase): def __init__(self, reactor, contextFactory=BrowserLikePolicyForHTTPS(), connectTimeout=None, bindAddress=None, pool=None): if not IPolicyForHTTPS.providedBy(contextFactory): warnings.warn( repr(contextFactory) + " was passed as the HTTPS policy for an Agent, but it does " "not provide IPolicyForHTTPS. Since Twisted 14.0, you must " "pass a provider of IPolicyForHTTPS.", stacklevel=2, category=DeprecationWarning ) contextFactory = _DeprecatedToCurrentPolicyForHTTPS(contextFactory) endpointFactory = _StandardEndpointFactory( reactor, contextFactory, connectTimeout, bindAddress) self._init(reactor, endpointFactory, pool) """ _AgentBase.__init__(self, reactor, pool) self._endpointFactory = endpointFactory @implementer(IAgentEndpointFactory) class _StandardEndpointFactory(object): @implementer(interfaces.IStreamClientEndpoint) class HostnameEndpoint(object): def resolvedAddressesToEndpoints(addresses): # Yield an endpoint for every address resolved from the name. for eachAddress in addresses: if isinstance(eachAddress, IPv6Address): yield TCP6ClientEndpoint( self._reactor, eachAddress.host, eachAddress.port, self._timeout, self._bindAddress ) if isinstance(eachAddress, IPv4Address): yield TCP4ClientEndpoint( self._reactor, eachAddress.host, eachAddress.port, self._timeout, self._bindAddress
agent相当于浏览器,agent的request方法通过完成一个请求。request会先dns,得到地址后生成tcp4clientendpoint(reactor.connectTCP())