简体   繁体   中英

Python-xmpp HostUnknown error during connect

There are other questions the same but none have a clear answer that actually functions. I am trying to send a chat message via 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)

However I do not understand the error that is returned:

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', '')

I can see that I am successfully connecting to the server, but after that it makes no sense. I suspect problems with the "client" in client = xmpp.Client('Adium') but not sure what should go there or if it is even an issue. What do you think?

I am using python-xmpp on python 2.7 and wonder if this is not compatible or something.

In your code you have this line:

client = xmpp.Client('Adium')

Which should be:

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

The parameter here is the XMPP host you are connecting to. Because 'Adium' is not a recognised host on the server, it is returning a host-unknown error, as per your logs:

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

An even better approach would be to rewrite your login code like this:

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')

Now there is only a jid variable which contains your XMPP address (known as a 'Jabber ID', or JID for short). jid.getDomain() returns the server part, and jid.getNode() returns the user part of JID.

I also removed the manual specification of the host and port from client.connect() . These should not be necessary if your server and DNS are configured correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM