Twisted:带有推送生产者示例的 tcp 服务器?

发布于 2024-08-08 11:22:58 字数 362 浏览 5 评论 0原文

我想使用 Python 和 Twisted 组合简单的 TCP 服务器。

服务器启动并等待连接 - 我已经有客户端 - 非 python 应用程序。一旦建立连接,服务器就会开始以一定的间隔(例如1秒)发送数据。

服务器从静态文件读取数据(一次一条记录),我应该能够弄清楚这部分。

我假设一旦客户端连接,我将使用推送生成器开始推送数据。

我有一个简单的tcp服务器,工厂是twisted,我可以对connectionMade/dataReceived等做出反应,但我不知道如何插入推送生产者

有人知道在twisted中显示带有tcp服务器的推送生产者的示例吗?

I want to put together simple TCP server using Python and Twisted.

The server starts up and waits for connection - I already have client - non-python application. Once connection is made server starts sending data at some interval (e.g. 1 sec).

The server reads data from a static file (a record at a time), I should be able to figure out this part.

I assume that I would use push producer to start pushing data once client is connected.

I have simple tcp server with factory in twisted and I can react to connectionMade/dataReceived and so on but I can't figure out how to plug in the push producer.

Anyone knows any examples showing push producer with tcp server in twisted?

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

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

发布评论

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

评论(2

旧竹 2024-08-15 11:22:58

这里是推送生成器的完整示例。它已被添加到twisted svn 中作为示例。

Here is a complete example of a push producer. It's been added to the twisted svn as an example.

末が日狂欢 2024-08-15 11:22:58

一些简单的事情怎么样:

thedata = '''
Questa mattina
mi son svegliato
o bella ciao, bella ciao,
bella ciao, ciao, ciao
questa mattina
mi son svegliato
ho trovato l'invasor!
'''.splitlines(True)

class Push(protocol.Protocol):
    """This is just about the simplest possible protocol"""
    def connectionMade(self):
        for line in thedata:
          if not line or line.isspace():
            continue
          self.transport.write(line)
          time.sleep(1.0)
        self.transport.loseConnection()

这有硬编码的数据,但你说从文件中读取它不是你的问题。如果您能告诉我们这个过于简单的“推送服务器”有什么问题,也许我们可以提供更好的帮助!-)

What about something simplistic like:

thedata = '''
Questa mattina
mi son svegliato
o bella ciao, bella ciao,
bella ciao, ciao, ciao
questa mattina
mi son svegliato
ho trovato l'invasor!
'''.splitlines(True)

class Push(protocol.Protocol):
    """This is just about the simplest possible protocol"""
    def connectionMade(self):
        for line in thedata:
          if not line or line.isspace():
            continue
          self.transport.write(line)
          time.sleep(1.0)
        self.transport.loseConnection()

This has hard-coded data, but you say that reading it from a file instead is not your problem. If you can tell us what's wrong with this overly simplistic "push server", maybe we can offer better help!-)

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