Twisted - SleekXMPP兼容性

时间:2012-12-21 10:18:15

标签: python xmpp twisted

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

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

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类来模拟即将到来的请求。 我尝试谷歌任何发布但没有结果。有没有人知道这里有什么问题?

1 个答案:

答案 0 :(得分:2)

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

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