在 python 中简单连续运行 XMPP 客户端

发布于 2024-10-04 18:23:06 字数 1400 浏览 1 评论 0原文

我正在使用 python-xmpp 发送 jabber 消息。一切工作正常,除了每次我想发送消息(每 15 分钟)我需要重新连接到 jabber 服务器,同时发送客户端处于离线状态并且无法接收消息。

所以我想编写一个非常简单的、无限期运行的 xmpp 客户端,它始终在线,并且可以在需要时发送(和接收)消息。

我的琐碎(无效)方法:

import time
import xmpp

class Jabber(object):
    def __init__(self):
        server = 'example.com'
        username = 'bot'
        passwd = 'password'
        self.client = xmpp.Client(server)
        self.client.connect(server=(server, 5222))
        self.client.auth(username, passwd, 'bot')
        self.client.sendInitPresence()
        self.sleep()

    def sleep(self):
        self.awake = False
        delay = 1
        while not self.awake:
            time.sleep(delay)

    def wake(self):
        self.awake = True

    def auth(self, jid):
        self.client.getRoster().Authorize(jid)
        self.sleep()

    def send(self, jid, msg):
        message = xmpp.Message(jid, msg)
        message.setAttr('type', 'chat')
        self.client.send(message)
        self.sleep()

if __name__ == '__main__':
    j = Jabber()
    time.sleep(3)
    j.wake()
    j.send('[email protected]', 'hello world')
    time.sleep(30)

这里的问题似乎是我无法唤醒它。我最好的猜测是我需要某种并发性。这是真的吗?如果是的话,我最好怎样做?

编辑:在研究了有关并发性的所有选项之后,我决定使用twisted和wokkel。如果可以的话,我会删除这篇文章。

I'm using python-xmpp to send jabber messages. Everything works fine except that every time I want to send messages (every 15 minutes) I need to reconnect to the jabber server, and in the meantime the sending client is offline and cannot receive messages.

So I want to write a really simple, indefinitely running xmpp client, that is online the whole time and can send (and receive) messages when required.

My trivial (non-working) approach:

import time
import xmpp

class Jabber(object):
    def __init__(self):
        server = 'example.com'
        username = 'bot'
        passwd = 'password'
        self.client = xmpp.Client(server)
        self.client.connect(server=(server, 5222))
        self.client.auth(username, passwd, 'bot')
        self.client.sendInitPresence()
        self.sleep()

    def sleep(self):
        self.awake = False
        delay = 1
        while not self.awake:
            time.sleep(delay)

    def wake(self):
        self.awake = True

    def auth(self, jid):
        self.client.getRoster().Authorize(jid)
        self.sleep()

    def send(self, jid, msg):
        message = xmpp.Message(jid, msg)
        message.setAttr('type', 'chat')
        self.client.send(message)
        self.sleep()

if __name__ == '__main__':
    j = Jabber()
    time.sleep(3)
    j.wake()
    j.send('[email protected]', 'hello world')
    time.sleep(30)

The problem here seems to be that I cannot wake it up. My best guess is that I need some kind of concurrency. Is that true, and if so how would I best go about that?

EDIT: After looking into all the options concerning concurrency, I decided to go with twisted and wokkel. If I could, I would delete this post.

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

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

发布评论

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

评论(2

找回味觉 2024-10-11 18:23:06

xmpppy 本身(这是 python-xmpp 的另一个名称)的 homepage 上有一个很好的示例,它确实几乎你想要的: xtalk.py

它基本上是一个控制台 jabber-client,但应该重写成你想要的机器人并不难。

它始终在线并且可以发送和接收消息。我认为这里不需要多处理(或其他并发)模块,除非您需要在完全相同的时间接收和发送消息。

There is a good example on the homepage of xmpppy itself (which is another name for python-xmpp), which does almost what you want: xtalk.py

It is basically a console jabber-client, but shouldn't be hard to rewrite into bot you want.

It's always online and can send and receive messages. I don't see a need for multiprocessing (or other concurrency) module here, unless you need to receive and send messages at exact same time.

欲拥i 2024-10-11 18:23:06

Process(timeout) 方法上进行循环是在保持连接的同时等待和处理任何新传入节的好方法。

A loop over the Process(timeout) method is a good way to wait and process any new incoming stanzas while keeping the connection up.

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