python - Twisted - SleekXMPP 兼容性

标签 python xmpp twisted

我正在用 python 制作一个 XMPP 中间件,它监听一个地址(主机、端口),当它在该端口上接收到一些连接时,它会向服务器上的 jid(XMPP 用户)发送一条 XMPP 消息。 快速回顾我的设置 对于网络部分,我使用的是扭曲的 对于 XMPP - SleekXMPP XMPP 服务器 - Openfire

现在,当我尝试使用 sleekXMPP 而不从 twistedm 导入任何东西时,它工作正常。 但是,我尝试在一个程序中混合使用 sleekXMPP 和 twisted(通过导入它们),但出现以下错误。

Traceback (most recent call last):
File "sleekXMPP/prog_3.py", line 124, in <module>
  main()
 File "sleekXMPP/prog_3.py", line 111, in main
  xmppThread = ClientThread(dir_q)
File "sleekXMPP/prog_3.py", line 41, in __init__
  self.xmpp = xmppClient(self.jid, self.password)
File "sleekXMPP/prog_3.py", line 17, in __init__
  sleekxmpp.ClientXMPP.__init__(self, jid, password)
File "build\bdist.win32\egg\sleekxmpp\clientxmpp.py", line 65, in __init__
File "build\bdist.win32\egg\sleekxmpp\basexmpp.py", line 72, in __init__
File "build\bdist.win32\egg\sleekxmpp\jid.py", line 461, in __init__
File "build\bdist.win32\egg\sleekxmpp\jid.py", line 150, in _parse_jid
File "build\bdist.win32\egg\sleekxmpp\jid.py", line 202, in _validate_domain
File "C:\Python27\lib\site-packages\twisted-12.2.0-py2.7-win32.egg\twisted\python \compat.py", line 22, in inet_pton
  raise ValueError("Illegal characters: %r" % (''.join(x),))
ValueError: Illegal characters: u't'

代码如下:

import sleekxmpp
import ssl
import Queue
import threading
import time
import logging
import traceback
import sys
from twisted.internet import reactor, protocol , endpoints
class xmppClient(sleekxmpp.ClientXMPP):
    """
This class defines the xmppClient object used to interact with the XMPP server 
"""
    def __init__(self, jid, password):
        # the constructor 
        sleekxmpp.ClientXMPP.__init__(self, jid, password)
        self.add_event_handler('session_start', self.start)
    def start(self, event):
        self.send_presence()
       self.get_roster()

    def send_note(self):
        self.mssg = r"Hello from XMPP Service"
        self.recipient = r"testuser2@ghost"
        self.send_message(mto=self.recipient,
                     mbody=self.mssg,
                      mtype='chat')
        print "Message sent"


class ClientThread(threading.Thread):
    def __init__(self, dir_q):
        super(ClientThread, self).__init__()
        self.dir_q = dir_q
        self.stoprequest = threading.Event()
        self.jid = 'testuser1@ghost'
        self.password = 'password'
        self.xmpp = xmppClient(self.jid, self.password)
        self.xmpp.register_plugin('xep_0030') # Service Discovery
        self.xmpp.register_plugin('xep_0004') # Data Forms
        self.xmpp.register_plugin('xep_0060') # PubSub
        self.xmpp.register_plugin('xep_0199') # XMPP Ping

        self.xmpp.ssl_version = ssl.PROTOCOL_SSLv3


        if self.xmpp.connect():
            print("Connected")
            self.xmpp.process(block=False)
        else:
            print("Unable to connect.")

    def run(self):
        while not self.stoprequest.isSet():
            try:
                req = self.dir_q.get(True, 0.05)
                if req == 1:
                    self.xmpp.send_note()
            except Queue.Empty:
                continue

    def join(self, timeout=None):
        self.stoprequest.set()
        super(ClientThread, self).join(timeout)

class reqSimulator(threading.Thread):
    def __init__(self, dir_q):
        super(reqSimulator, self).__init__()
        self.dir_q = dir_q
    def run(self):
            while(1):
                self.dir_q.put(1)
                time.sleep(0.5)
"""class sendProtocol(protocol.Protocol):
      def connectionMade(self):
          r = reqSimulator(self.factory.dir_q)
          r.run()
      def connectionLost(self, reason):
         pass

     def dataReceived(self, data):
         self.transport.loseConnection()"""

def main():
    logging.basicConfig()
    dir_q = Queue.Queue()
    xmppThread = ClientThread(dir_q)
    xmppThread.start()
    sim = reqSimulator(dir_q)
    """factory = protocol.ServerFactory()
    factory.dir_q = dir_q
    factory.protocol = sendProtocol
    endpoints.serverFromString(reactor, "tcp:8001").listen(factory) 
   reactor.run()
   """
  sim.start()

if __name__ == "__main__":
    main()

请注意,在此代码中,注释了实际的网络代码,并且我正在使用 reqSimulator 类来模拟即将到来的请求。 我试图用谷歌搜索任何已发布但没有结果。有人知道这里出了什么问题吗?

最佳答案

这是因为 Twisted 为 Windows 实现了 inet_pton,但使用与 stdlib 版本不同的失败异常。

我已经在 github 上修复了 Sleek(master 和 develop 分支),很快就会有一个新的点发布。

关于python - Twisted - SleekXMPP 兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988224/

相关文章:

Android smack 服务器回执确认

python - Twisted 如何与命令行程序交互? Node.js 可以吗?

python - Twisted Python 失败 - Scrapy 问题

python - 使用日志记录将消息记录到数组/列表

python - 使用mysql syncdb错误

Python3 使用 lambda 对嵌套字典的列表进行排序

xmpp - 如何在 smack 4.1 beta2 中创建持久的 muc 空间

python - pandas系列中的两列排序

android - 适用于 Android 的 XMPP/Jingle 语音库

python - 如何知道 react 堆所有过程是否完成