连接期间的Python-xmpp HostUnknown错误
Posted
技术标签:
【中文标题】连接期间的Python-xmpp HostUnknown错误【英文标题】:Python-xmpp HostUnknown error during connect 【发布时间】:2015-04-05 21:03:29 【问题描述】:还有其他相同的问题,但没有一个具有实际作用的明确答案。我正在尝试通过 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') 中的“客户端”存在问题,但不确定应该去那里,或者它是否是一个问题。你怎么看?
我在 python 2.7 上使用 python-xmpp,想知道这是否不兼容。
【问题讨论】:
当错误消息清楚地显示“主机未知”时,是什么让您认为您已成功连接? 我以为我在第 7 行注意到了。您的回答至少会更有帮助。 注意不要过分强调,这只是一个调试信息。而且我还没有发布答案,因为您的问题仍然不清楚。例如,您使用的是哪个 xmpp 库? 我正在使用 python-xmpp。问题很清楚。我想连接,不能。我可以使用标准客户端进行正常连接,以便验证我的端口和服务器是否已设置。不,我只需要让脚本也可以工作。系统提供调试显示连接错误,但我无法读取调试。 【参考方案1】:在你的代码中有这一行:
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()
返回服务器部分,jid.getNode()
返回 JID 的用户部分。
我还从client.connect()
中删除了主机和端口的手动规范。如果您的服务器和 DNS 配置正确,则不需要这些。
【讨论】:
干得好,这很有帮助。我现在让它工作了。客户是最初的问题。我找不到应该在哪里定义的地方。再次感谢以上是关于连接期间的Python-xmpp HostUnknown错误的主要内容,如果未能解决你的问题,请参考以下文章
python-xmpp 并循环接收要接收的收件人列表和 IM 消息