Question

How can i access underlying socket from twisted.web.client.Agent? I need to enable TCP_NODELAY on this socket.

Was it helpful?

Solution

Unfortunately Agent doesn't make it as easy as it would be if you were working directly with a Protocol instance, but it's not impossible either.

The key lies here, in the class definition of Agent:

_protocol = HTTP11ClientProtocol

In order to get access to the transport you could override connectionMade on HTTP11ClientProtocol, as well as the Agent.

So you'd end up with something like:

from twisted.web import client
class MyHTTPClient(client.HTTP11ClientProtocol):
    def connectionMade(self):
        self.transport.setTcpNoDelay(True)
        client.HTTP11ClientProtocol.connectionMade(self) # call the super-class's connectionMade

class MyAgent(client.Agent):
    _protocol = MyHTTPClient

Now use MyAgent in lieu of Agent and you'll get TCP nodelay on the client.

** Note **, this isn't the only way to do this, but one way you can do so and continue to use Agent.request. Alternately, write your own agent which crafts the request and connects it to a Client and wires up your request, along with TCP nodelay, in a deferred chain.

** Note 2 ** In this case, it's fine to assume 'transport' has the setTcpNoDelay() method because it's a pretty reasonable assumption you'll be using TCP as the transport for an HTTP request. This may not be a smart idea all over twisted, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top