2012-12-21 19 views
3

我正在python中偵聽一個地址(主機,端口)的XMPP中間件,並且當它在該端口上收到某個連接時,它會發送一個XMPP消息給一個jid(XMPP用戶)服務器。 我的設置 的快速審查網絡的一部分,我使用雙絞線 對於XMPP - SleekXMPP XMPP服務器 - Openfire的Twisted - SleekXMPP兼容性

現在,當我嘗試使用sleekXMPP未經twistedm導入任何它是工作的罰款。 但是,我嘗試混合sleekXMPP和扭曲在一個程序(通過導入它們)我得到follwing錯誤。

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"[email protected]" 
     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 = '[email protected]' 
     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類來模擬迎面而來的請求。 我試圖谷歌任何發佈,但沒有得到任何結果。有人知道這裏有什麼問題嗎?

+0

你的代碼在哪裏? –

+0

@ZagorulkinDmitry添加了代碼:-) – mlakhara

+0

您使用的是什麼版本的SleekXMPP? –

回答

2

這是由於Twisted實現了inet_pton for Windows的事實,但使用了不同於stdlib版本的異常故障。

我已經修復了這個(主和發展分支)github上的圓滑,並且會有一個新的點發布它不久。

+1

感謝您的決議。這是問題。然而在resolver.py中有同樣的問題,我修改了我的程序後爲我工作。我也曾在github上提過這個問題。 https://github.com/fritzy/SleekXMPP/issues/213 – mlakhara