连接期间的Python-xmpp HostUnknown错误

时间:2015-04-05 21:03:29

标签: python xmpp

还有其他问题相同,但没有一个问题具有明确的功能。我试图通过python-xmpp发送聊天消息:

import xmpp

username = 'test@server.com'
passwd = 'password'
to='test2@server.com'
msg='hello :)'


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

但是我不明白返回的错误:

Invalid debugflag given: always
Invalid debugflag given: nodebuilder
DEBUG: 
DEBUG: Debug created for /usr/lib/python2.7/dist-packages/xmpp/client.py
DEBUG:  flags defined: always,nodebuilder
An error occurred while looking up _xmpp-client._tcp.server.com
DEBUG: socket       start Plugging <xmpp.transports.TCPsocket instance at 0xc04940> into <xmpp.client.Client instance at 0xc04350>
DEBUG: socket       start Successfully connected to remote host ('server.com', 5222)
DEBUG: dispatcher   start Plugging <xmpp.dispatcher.Dispatcher instance at 0xc04af8> into <xmpp.client.Client instance at 0xc04350>
DEBUG: dispatcher   info  Registering namespace "unknown"
DEBUG: dispatcher   info  Registering protocol "unknown" as xmpp.protocol.Protocol(unknown)
DEBUG: dispatcher   info  Registering protocol "default" as xmpp.protocol.Protocol(unknown)
DEBUG: dispatcher   info  Registering namespace "http://etherx.jabber.org/streams"
DEBUG: dispatcher   info  Registering protocol "unknown" as xmpp.protocol.Protocol(http://etherx.jabber.org/streams)
DEBUG: dispatcher   info  Registering protocol "default" as xmpp.protocol.Protocol(http://etherx.jabber.org/streams)
DEBUG: dispatcher   info  Registering namespace "jabber:client"
DEBUG: dispatcher   info  Registering protocol "unknown" as xmpp.protocol.Protocol(jabber:client)
DEBUG: dispatcher   info  Registering protocol "default" as xmpp.protocol.Protocol(jabber:client)
DEBUG: dispatcher   info  Registering protocol "iq" as xmpp.protocol.Iq(jabber:client)
DEBUG: dispatcher   info  Registering protocol "presence" as xmpp.protocol.Presence(jabber:client)
DEBUG: dispatcher   info  Registering protocol "message" as xmpp.protocol.Message(jabber:client)
DEBUG: dispatcher   info  Registering handler <bound method Dispatcher.streamErrorHandler of <xmpp.dispatcher.Dispatcher instance at 0xc04af8>> for "error" type-> ns->(http://etherx.jabber.org/streams)
DEBUG: dispatcher   warn  Registering protocol "error" as xmpp.protocol.Protocol(http://etherx.jabber.org/streams)
DEBUG: socket       sent  <?xml version='1.0'?>
  <stream:stream xmlns="jabber:client" to="Adium" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" >
DEBUG: socket       got   <?xml version='1.0'?>
  <stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='4001548908' from='server.com' xml:lang='en'>
  <stream:error>
  <host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
  </stream:error>
  </stream:stream>
DEBUG: dispatcher   ok    Got http://etherx.jabber.org/streams/error stanza
DEBUG: dispatcher   ok    Dispatching error stanza with type-> props->[u'urn:ietf:params:xml:ns:xmpp-streams'] id->None
Traceback (most recent call last):
  File "chat2.py", line 10, in <module>
    client.connect(server=('server.com',5222))
  File "/usr/lib/python2.7/dist-packages/xmpp/client.py", line 200, in connect
    if not CommonClient.connect(self,server,proxy,secure,use_srv) or secure<>None and not secure: return self.connected
  File "/usr/lib/python2.7/dist-packages/xmpp/client.py", line 184, in connect
    if not self.Process(1): return
  File "/usr/lib/python2.7/dist-packages/xmpp/dispatcher.py", line 303, in dispatch
    handler['func'](session,stanza)
  File "/usr/lib/python2.7/dist-packages/xmpp/dispatcher.py", line 215, in streamErrorHandler
    raise exc((name,text))
xmpp.protocol.HostUnknown: (u'host-unknown', '')

我可以看到我成功连接到服务器,但之后就没有意义了。我怀疑客户端= xmpp.Client('Adium')中的“客户端”存在问题,但不确定应该去哪里或者甚至是问题。你觉得怎么样?

我在python 2.7上使用python-xmpp并想知道这是不兼容的还是什么。

1 个答案:

答案 0 :(得分:0)

在你的代码中你有这一行:

client = xmpp.Client('Adium')

应该是:

client = xmpp.Client('server.com')

此处的参数是您要连接的XMPP主机。因为&#39; Adium&#39;不是服务器上可识别的主机,它根据您的日志返回主机未知错误:

   <stream:error>
   <host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
   </stream:error>

更好的方法是重写您的登录代码:

import xmpp

jid = xmpp.protocol.JID('test@server.com')
passwd = 'password'
to='test2@server.com'
msg='hello :)'

client = xmpp.Client(jid.getDomain())
client.connect()
client.auth(jid.getNode(), passwd, 'botty')

现在只有一个jid变量包含您的XMPP地址(简称为Jabber ID&#39;或简称JID)。 jid.getDomain()返回服务器部分,jid.getNode()返回JID的用户部分。

我还从client.connect()删除了主机和端口的手动规范。如果您的服务器和DNS配置正确,则不需要这些。