简体   繁体   English

twisted.web.client.Agent 的访问套接字选项

[英]Access socket options for twisted.web.client.Agent

How can i access underlying socket from twisted.web.client.Agent?如何从 twisted.web.client.Agent 访问底层套接字? I need to enable TCP_NODELAY on this socket.我需要在这个套接字上启用 TCP_NODELAY。

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.不幸的是,如果您直接使用协议实例,Agent 并没有让它变得那么容易,但这也不是不可能的。

The key lies here, in the class definition of Agent:关键就在这里,在Agent的class定义中:

_protocol = HTTP11ClientProtocol

In order to get access to the transport you could override connectionMade on HTTP11ClientProtocol, as well as the Agent.为了访问传输,您可以覆盖 HTTP11ClientProtocol 上的 connectionMade 以及代理。

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.现在使用 MyAgent 代替 Agent,您将在客户端上获得 TCP nodelay。

** Note **, this isn't the only way to do this, but one way you can do so and continue to use Agent.request. ** 注意 **,这不是执行此操作的唯一方法,而是您可以这样做并继续使用 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.或者,编写您自己的代理来制作请求并将其连接到客户端并将您的请求与 TCP 节点层一起连接到延迟链中。

** 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. ** 注意 2 ** 在这种情况下,可以假设“传输”具有setTcpNoDelay()方法,因为这是一个非常合理的假设,您将使用 TCP 作为 HTTP 请求的传输。 This may not be a smart idea all over twisted, though.不过,这可能不是一个完全扭曲的聪明想法。

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

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