How can i access underlying socket from twisted.web.client.Agent? I need to enable TCP_NODELAY on this socket.
1 Answers
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.

- 15,996
- 5
- 45
- 53
-
1Don't forget that `_` means "private". – Jean-Paul Calderone Apr 28 '11 at 11:38
-
@JP: The pragmatist in me is often at war with the library maintainer in me who wants to not violate API conventions. Unfortunately, the pragmatist often wins. Since you have a more personal knowledge of Twisted, do you happen to know why in the case of Client the "protocol" attribute is flagged private? This is somewhat incongruent with many other factory-type objects. – Crast May 03 '11 at 22:12