Python-xmpp HostUnknown 连接时出错
Python-xmpp HostUnknown error during connect
还有其他相同的问题,但 none 有一个明确的答案,确实有效。我正在尝试通过 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', '')
我可以看到我已成功连接到服务器,但之后就没有任何意义了。我怀疑 client = xmpp.Client('Adium') 中的 "client" 有问题,但不确定那里应该放什么,或者它是否是一个问题。你怎么看?
我在 python 2.7 上使用 python-xmpp,想知道这是否不兼容或其他问题。
在您的代码中有这一行:
client = xmpp.Client('Adium')
应该是:
client = xmpp.Client('server.com')
这里的参数是你要连接的XMPP主机。因为 'Adium' 不是服务器上可识别的主机,根据您的日志,它返回主机未知错误:
<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',简称 JID)。 jid.getDomain()
returns 服务器部分,jid.getNode()
returns JID 的用户部分。
我还从 client.connect()
中删除了主机和端口的手动指定。如果您的服务器和 DNS 配置正确,则不需要这些。
还有其他相同的问题,但 none 有一个明确的答案,确实有效。我正在尝试通过 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', '')
我可以看到我已成功连接到服务器,但之后就没有任何意义了。我怀疑 client = xmpp.Client('Adium') 中的 "client" 有问题,但不确定那里应该放什么,或者它是否是一个问题。你怎么看?
我在 python 2.7 上使用 python-xmpp,想知道这是否不兼容或其他问题。
在您的代码中有这一行:
client = xmpp.Client('Adium')
应该是:
client = xmpp.Client('server.com')
这里的参数是你要连接的XMPP主机。因为 'Adium' 不是服务器上可识别的主机,根据您的日志,它返回主机未知错误:
<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',简称 JID)。 jid.getDomain()
returns 服务器部分,jid.getNode()
returns JID 的用户部分。
我还从 client.connect()
中删除了主机和端口的手动指定。如果您的服务器和 DNS 配置正确,则不需要这些。