用于允许断开连接的仅发送协议的扭曲客户端

发布于 2024-09-27 19:23:51 字数 385 浏览 0 评论 0原文

我决定在twisted 的帮助下涉足异步Python 的世界。我已经实现了文档中的一些示例,但是我很难找到我正在尝试编写的非常简单的客户端示例。

简而言之,我想要一个客户端与服务器建立 TCP 连接,然后将简单的“\n”终止字符串消息从队列对象发送到服务器。服务器不会响应任何消息,因此我的客户端是完全单向的。我/认为/我想要的是这个示例的某种组合 和twisted.internet.protocols.basic.LineReceiver 便利协议。感觉这应该是扭曲中可以做的最简单的事情,但我在网上看到的文档或示例似乎都不太合适。

I've decided to dip my toe into the world of asynchronous python with the help of twisted. I've implemented some of the examples from the documentation, but I'm having a difficult time finding an example of the, very simple, client I'm trying to write.

In short I'd like a client which establishes a tcp connection with a server and then sends simple "\n" terminated string messages off of a queue object to the server. The server doesn't ever respond with any messages so my client is fully unidirectional. I /think/ that what I want is some combination of this example and the twisted.internet.protocols.basic.LineReceiver convenience protocol. This feels like it should be just about the simplest thing one could do in twisted, but none of the documentation or examples I've seen online seem to fit quite right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2024-10-04 19:23:51

我所做的没有使用队列,但我正在说明一旦建立连接就发送一行的代码。有很多印刷材料可以帮助您了解正在发生的事情。

常见的导入内​​容:

from twisted.web import proxy
from twisted.internet import reactor
from twisted.internet import protocol
from twisted.internet.protocol import ReconnectingClientFactory 
from twisted.protocols import basic
from twisted.python import log
import sys
log.startLogging(sys.stdout)

您创建一个从线路接收器派生的协议,设置分隔符。
在这种情况下,一旦建立连接,我只需编写一个字符串“www”。
关键是查看twisted.internet.interface.py 上的协议接口并了解协议的各种方法以及它们的作用和调用时间。

class MyProtocol(basic.LineReceiver):

    #def makeConnection(self, transport):
    #    print transport       

    def connectionLost(self, reason):
        print reason
        self.sendData = False

    def connectionMade(self):
        print "connection made"
        self.delimiter = "\n"
        self.sendData = True
        print self.transport
        self.sendFromQueue()

    def sendFromQueue(self):
        while self.sendData:
            msg = dataQueue.get()
            self.sendLine(msg)
            # you need to handle empty queue
            # Have another function to resume 

最后,一个协议工厂将为每个连接创建一个协议实例。
查看方法:buildProtcol。

class myProtocolFactory():
    protocol = MyProtocol

    def doStart(self):
        pass

    def startedConnecting(self, connectorInstance):
        print connectorInstance

    def buildProtocol(self, address):
        print address
        return self.protocol()

    def clientConnectionLost(self, connection, reason):
        print reason
        print connection

    def clientConnectionFailed(self, connection, reason):
        print connection
        print reason

    def doStop(self):
        pass

现在您使用连接器来建立连接:

reactor.connectTCP('localhost', 50000, myProtocolFactory())
reactor.run()

我运行了它并将其连接到一个服务器,该服务器仅打印它收到的内容,因此不发送回确认。这是输出:

1286906080.08   82     INFO 140735087148064 __main__ conn_made: client_address=127.0.0.1:50277
1286906080.08   83    DEBUG 140735087148064 __main__ created handler; waiting for loop
1286906080.08   83    DEBUG 140735087148064 __main__ handle_read
1286906080.08   83    DEBUG 140735087148064 __main__ after recv
'www\n'

Recieved: 4

上面的例子如果不是容错的。要重新连接,当连接丢失时,您可以从现有的扭曲类 - ReconnectingClientFactory 派生协议工厂。
Twisted 几乎拥有您需要的所有工具:)

class myProtocolFactory(ReconnectingClientFactory):
    protocol = MyProtocol

    def buildProtocol(self, address):
        print address
        return self.protocol()

供进一步参考

我建议您阅读:http://krondo.com/?page_id=1327

[编辑:根据下面的评论]

What I have done is not used a Queue but I am illustrating the code that sends a line, once a connection is made. There are bunch of print stuff that will help you understand on what is going on.

Usual import stuff:

from twisted.web import proxy
from twisted.internet import reactor
from twisted.internet import protocol
from twisted.internet.protocol import ReconnectingClientFactory 
from twisted.protocols import basic
from twisted.python import log
import sys
log.startLogging(sys.stdout)

You create a protocol derived from line receiver, set the delimiter.
In this case, I simply write a string "www" once the connection is made.
The key thing is to look at protocol interface at twisted.internet.interface.py and understand the various methods of protocol and what they do and when they are called.

class MyProtocol(basic.LineReceiver):

    #def makeConnection(self, transport):
    #    print transport       

    def connectionLost(self, reason):
        print reason
        self.sendData = False

    def connectionMade(self):
        print "connection made"
        self.delimiter = "\n"
        self.sendData = True
        print self.transport
        self.sendFromQueue()

    def sendFromQueue(self):
        while self.sendData:
            msg = dataQueue.get()
            self.sendLine(msg)
            # you need to handle empty queue
            # Have another function to resume 

Finally, A protocol factory that will create a protocol instance for every connection.
Look at method : buildProtcol.

class myProtocolFactory():
    protocol = MyProtocol

    def doStart(self):
        pass

    def startedConnecting(self, connectorInstance):
        print connectorInstance

    def buildProtocol(self, address):
        print address
        return self.protocol()

    def clientConnectionLost(self, connection, reason):
        print reason
        print connection

    def clientConnectionFailed(self, connection, reason):
        print connection
        print reason

    def doStop(self):
        pass

Now you use a connector to make a connection:

reactor.connectTCP('localhost', 50000, myProtocolFactory())
reactor.run()

I ran this and connected it to an server that simply prints what it receives and hence send no ack back. Here is the output:

1286906080.08   82     INFO 140735087148064 __main__ conn_made: client_address=127.0.0.1:50277
1286906080.08   83    DEBUG 140735087148064 __main__ created handler; waiting for loop
1286906080.08   83    DEBUG 140735087148064 __main__ handle_read
1286906080.08   83    DEBUG 140735087148064 __main__ after recv
'www\n'

Recieved: 4

The above example if not fault tolerant. To reconnect , when a connection is lost, you can derive your protocol factory from an existing twisted class - ReconnectingClientFactory.
Twisted has almost all the tools that you would need :)

class myProtocolFactory(ReconnectingClientFactory):
    protocol = MyProtocol

    def buildProtocol(self, address):
        print address
        return self.protocol()

For further reference

I suggest that you read : http://krondo.com/?page_id=1327

[Edited: As per comment below]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文