在后台使用 Twisted 代理 TCP 套接字的一般方法?
我正在寻找一些关于如何解决我认为 Twisted 非常适合的问题的一般信息。 (我是 Twisted 的新手,但不是 Python)
我有一个家庭自动化控制器,可以支持单个 TCP 套接字连接,发送和接收二进制数据。我想使用 XMPP 作为套接字的桥梁,以便用户可以发送命令和接收事件。
我得到了一个与 Twisted 一起使用的基本套接字连接,它能够从 O'Reilly 书中的示例之一发送和接收命令。我还有一个使用 SleekXMPP 库编写的完全可用的 Python XMPP 机器人,我对此很满意。我只是不确定如何将这些结合在一起。
基本场景是:
- 用户向 XMPP 机器人发送消息,XMPP 机器人确定要发送到套接字的命令
- ASCII Socket 命令转换为二进制并发送到套接字
- Socket 接收命令并发送二进制响应
- 二进制响应转换为 ASCII
- XMPP 机器人发送回响应给用户。
- 网络事件(独立于用户操作)也可以由网络套接字接收,并应该发送给用户。
#6 提出了挑战,否则我只需在需要写入内容时按需打开/关闭套接字。
我在 Twisted 中遇到困难的部分是使这两个事件循环进行通信的最佳方法。我看过很多关于使用队列、延迟、线程、选择等的信息。我有一种感觉,如果我学会正确使用该工具,Twisted 可以处理大部分复杂性。
如果有人能给我指明正确的方向,我就会带球跑。正如我提到的,我对我的 XMPP 机器人很满意,并且我想使用现有的代码。我认为我的问题现在归结为在后台创建套接字,然后在前台从该套接字发送和接收数据。
顺便说一句,我很高兴在代码运行后将其分享回来,这样其他人就可以从我所寻求的帮助中受益。
——斯科特
I'm looking for some general information on how I should approach a problem that I think Twisted is a great fit for. (I'm new to Twisted but not Python)
I have a home automation controller that can support a single TCP socket connection, sending and receiving binary data. I'd like to use XMPP as a bridge to the socket so a user can send commands and receive events.
I got a rudimentary socket connection working with Twisted that was able to send and receive commands from one of the examples in the O'Reilly book. I also have a fully working Python XMPP bot written with the SleekXMPP library that I'm happy with. I'm just not sure how to bring these together.
The basic scenario is:
- User sends message to XMPP bot, which figures out what command to send to the socket
- ASCII Socket command is converted to binary and sent to socket
- Socket receives command and sends binary response
- Binary response converted to ASCII
- XMPP bot sends response back to user.
- Network events (independent from user action) can also be received by network socket and should be sent to user
It's #6 that is presenting the challenge, otherwise I'd just open/close the socket on demand when in need to write something.
The part that I'm having trouble wrapping my head around with Twisted is the best approach to make these two event loops communicate. I've seen lots of info on using Queues, deferred, threads, select, etc. I have a feeling that Twisted can handle much of the complexity if I just learn to use the tool properly.
If someone can point me in the right direction, I'll take the ball and run with it. As I mentioned, I'm happy with my XMPP bot and I'd like to use the existing code. I think my problem now comes down to creating the socket in the background, then sending and receiving data from that socket in the foreground.
By the way, I'm very happy to share back my code once it's working so someone else can benefit from the help I'm asking for.
-- Scott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非阻塞 IO 引擎的问题之一是它几乎是全有或全无。一旦引入阻塞代码,您很快就会失去事件驱动异步方法的大部分好处。只要有可能(根据经验),最好让整个应用程序在同一个反应器上运行。
在我看来,你有两个选择:
One of the problems with a non-blocking IO engine is that its pretty much all-or-nothing. As soon as you introduce blocking code, you can quickly lose most of the benefits of the event-driven asynch approach. Wherever possible (as a rule of thumb), its best to have the entire app running off the same reactor.
As i see it, you have two options: