简体   繁体   English

使用python库发送xmpp消息

[英]Send an xmpp message using a python library

How can I send an XMPP message using one of the following Python libraries: wokkel, xmpppy, or jabber.py ? 如何使用以下Python库之一发送XMPP消息:wokkel,xmpppy或jabber.py?

I think I am aware of the pseudo-code, but so far have not been able to get one running correctly. 我想我知道伪代码,但到目前为止还没能正确运行。 This is what I have tried so far: 这是我到目前为止所尝试的:

  • Call some API and pass the servername and port number to connect to that server. 调用一些API并传递服务器名称和端口号以连接到该服务器。
  • Call some API and pass the username, password to construct a JID object. 调用一些API并传递用户名,密码来构造JID对象。
  • Authenticate with that JID. 使用该JID进行身份验证。
  • Construct a Message object and call some API and pass that message obj in the argument. 构造一个Message对象并调用一些API并在参数中传递该消息obj。
  • Call some send API. 调用一些send API。

It seems easy enough in concept, but the devil is somewhere in the details. 在概念上似乎很容易,但魔鬼在细节中的某个地方。 Please show a sample snippet if that's possible. 如果可能,请显示示例代码段。

This is the simplest possible xmpp client. 这是最简单的xmpp客户端。 It will send a 'hello :)' message. 它会发送一个'hello :)'消息。 I'm using xmpppy in the example. 我在示例中使用了xmpppy And connecting to gtalk server. 并连接到gtalk服务器。 I think the example is self-explanatory: 我认为这个例子是不言自明的:

import xmpp

username = 'username'
passwd = 'password'
to='name@example.com'
msg='hello :)'


client = xmpp.Client('gmail.com')
client.connect(server=('talk.google.com',5223))
client.auth(username, passwd, 'botty')
client.sendInitPresence()
message = xmpp.Message(to, msg)
message.setAttr('type', 'chat')
client.send(message)

xmpppy has a number of examples listed on its main page (under "examples"), the most basic of which sends a single test message . xmpppy在其主页上列出了许多示例(在“示例”下),其中最基本的示例发送单个测试消息 They make the examples progressively more interesting -- they introduce the callback-oriented API via a chat bot program . 他们使示例逐渐变得更有趣 - 他们通过聊天机器人程序引入了面向回调的API。

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

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