跳转到主要内容

SleekXMPP是一个优雅的Python库,用于XMPP(也称为Jabber、Google Talk等)。

项目描述

SleekXMPP是Python 2.6/3.1+的MIT许可证XMPP库,并在Kevin Smith、Remko Tronçon和Peter Saint-Andre的《XMPP: definitive guide》的示例中有所展示。如果您是从阅读definitive guide到达这里的,请参阅有关将示例更新到SleekXMPP最新版本的说明。

SleekXMPP的设计目标和理念是

依赖项数量少

SleekXMPP的安装和使用应该尽可能简单,无需处理长的依赖链。

为了减少依赖项的数量,SleekXMPP在thirdparty目录中包含了一些第三方模块。从该模块导入时,首先尝试导入已安装的现有版本,然后加载包装版本,如果可能的话。

每个XEP都是一个插件

遵循Python的“包含电池”方法,目标是提供对当前所有活动XEPs(最终和草案)的支持。由于XEP支持是通过易于创建的插件实现的,因此希望也为实现和创建实验性XEPs提供一个坚实的基础。

令人愉悦的工作体验

尽可能多,SleekXMPP应允许使用合理的默认值和适当的抽象来“直接工作”。XML可以很丑陋,但不必如此。

获取代码

从PyPI获取最新稳定版本

pip install sleekxmpp

SleekXMPP的最新源代码可以在Github上找到。发布版本可以在master分支中找到,而最新开发版本在develop分支中。

最新发布
开发发布

安装DNSPython

如果您使用Python3并希望使用dnspython,您将需要检出并安装python3分支

git clone http://github.com/rthalley/dnspython
cd dnspython
git checkout python3
python3 setup.py install

讨论

有一个邮件列表和XMPP聊天室可供讨论和使用SleekXMPP的帮助。

邮件列表

SleekXMPP在Google Groups上的讨论

聊天

sleek@conference.jabber.org

文档和测试

文档可以在代码中找到,也可以在/docs中的Sphinx项目中找到。要生成Sphinx文档,请按照以下命令操作。HTML输出将在docs/_build/html中。

cd docs
make html
open _build/html/index.html

运行SleekXMPP的测试套件

python testall.py

SleekXMPP样板

使用SleekXMPP的项目通常会遵循设置客户端/组件连接和配置的基本模式。以下是SleekXMPP项目所需的样板文件的精髓。有关SleekXMPP项目的更详细的原型,请参阅文档或示例目录。

import logging

from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout


class EchoBot(ClientXMPP):

    def __init__(self, jid, password):
        ClientXMPP.__init__(self, jid, password)

        self.add_event_handler("session_start", self.session_start)
        self.add_event_handler("message", self.message)

        # If you wanted more functionality, here's how to register plugins:
        # self.register_plugin('xep_0030') # Service Discovery
        # self.register_plugin('xep_0199') # XMPP Ping

        # Here's how to access plugins once you've registered them:
        # self['xep_0030'].add_feature('echo_demo')

        # If you are working with an OpenFire server, you will
        # need to use a different SSL version:
        # import ssl
        # self.ssl_version = ssl.PROTOCOL_SSLv3

    def session_start(self, event):
        self.send_presence()
        self.get_roster()

        # Most get_*/set_* methods from plugins use Iq stanzas, which
        # can generate IqError and IqTimeout exceptions
        #
        # try:
        #     self.get_roster()
        # except IqError as err:
        #     logging.error('There was an error getting the roster')
        #     logging.error(err.iq['error']['condition'])
        #     self.disconnect()
        # except IqTimeout:
        #     logging.error('Server is taking too long to respond')
        #     self.disconnect()

    def message(self, msg):
        if msg['type'] in ('chat', 'normal'):
            msg.reply("Thanks for sending\n%(body)s" % msg).send()


if __name__ == '__main__':
    # Ideally use optparse or argparse to get JID,
    # password, and log level.

    logging.basicConfig(level=logging.DEBUG,
                        format='%(levelname)-8s %(message)s')

    xmpp = EchoBot('somejid@example.com', 'use_getpass')
    xmpp.connect()
    xmpp.process(block=True)

致谢

主要作者: Nathan Fritz

fritzy@netflint.net@fritzy

Nathan也是XMPPHP和Seesmic-AS3-XMPP的作者,并且曾是XMPP理事会的成员。

合著者: Lance Stout

lancestout@gmail.com@lancestout

贡献者

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

sleekxmpp-1.3.3.tar.gz (845.0 kB 查看哈希值)

上传时间 源代码

由以下机构支持