Python XMPP简单客户端错误

6
我遇到了以下错误:
AttributeError: Client instance has no attribute 'Dispatcher'

在Python 2.7中运行以下代码时:
import xmpp 

user= 'uname@gmail.com'
password="pass"

jid = xmpp.JID(user) 
connection = xmpp.Client(jid.getDomain()) 
connection.connect() 
connection.auth(jid.getNode(),password)

如果有人知道如何修复它,我会很高兴。

P.S. 在 N3RO 提出修复建议之后,错误的完整回溯如下:

C:\Users\krasnovi\Desktop\temp\xmpp tests>python xmpp.client.py
Invalid debugflag given: always
Invalid debugflag given: nodebuilder
DEBUG:
DEBUG: Debug created for build\bdist.win-amd64\egg\xmpp\client.py
DEBUG:  flags defined: always,nodebuilder
DEBUG: socket       start Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027C1708> into <xmpp.client.Client instance at 0x00000000027C1588>
DEBUG: socket       warn  An error occurred while looking up _xmpp-client._tcp.t
alk.gmail.com
DEBUG: socket       error Failed to connect to remote host ('talk.gmail.com', 52
23): getaddrinfo failed (11004)
Traceback (most recent call last):
  File "build\bdist.win-amd64\egg\xmpp\transports.py", line 133, in connect
    self._sock.connect((server[0], int(server[1])))
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
gaierror: [Errno 11004] getaddrinfo failed
DEBUG: socket       stop  Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027C1708> out of <xmpp.client.Client instance at 0x00000000027C1588>.
Traceback (most recent call last):
  File "xmpp.client.py", line 11, in <module>
    connection.auth(jid.getNode(),password)
  File "build\bdist.win-amd64\egg\xmpp\client.py", line 214, in auth
AttributeError: Client instance has no attribute 'Dispatcher'

修复之前:

Invalid debugflag given: always
Invalid debugflag given: nodebuilder
DEBUG:
DEBUG: Debug created for build\bdist.win-amd64\egg\xmpp\client.py
DEBUG:  flags defined: always,nodebuilder
DEBUG: socket       start Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027ED708> into <xmpp.client.Client instance at 0x00000000027ED588>
DEBUG: socket       error Failed to connect to remote host ('xmpp.l.google.com.'
, 5222): A connection attempt failed because the connected party did not properl
y respond after a period of time, or established connection failed because conne
cted host has failed to respond (10060)
Traceback (most recent call last):
  File "build\bdist.win-amd64\egg\xmpp\transports.py", line 133, in connect
    self._sock.connect((server[0], int(server[1])))
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10060] A connection attempt failed because the connected party did
 not properly respond after a period of time, or established connection failed b
ecause connected host has failed to respond
DEBUG: socket       stop  Plugging <xmpp.transports.TCPsocket instance at 0x0000
0000027ED708> out of <xmpp.client.Client instance at 0x00000000027ED588>.
Traceback (most recent call last):
  File "xmpp.client.py", line 11, in <module>
    connection.auth(jid.getNode(),password)
  File "build\bdist.win-amd64\egg\xmpp\client.py", line 214, in auth
AttributeError: Client instance has no attribute 'Dispatcher'

请包含完整的回溯跟踪以查找错误。 - Martijn Pieters
@Martijn Pieters。我做了。 - rok
1
AttributeError似乎是在连接由于超时而失败之后出现的问题。你是否在防火墙后面? - Martijn Pieters
@Martijn Pieters。抱歉,我回复有些晚了。我已关闭Windows防火墙,但仍然遇到相同的错误。这台计算机连接到域网络。也许,仍有一种方法可以通过编程成功连接?在运行程序时运行wireshark,发现只发送了3个SYN消息到173.194.70.125,就像我使用Google Talk连接时一样,但是没有捕获到响应。 - rok
2个回答

3
您需要指定要连接到的服务器。
connection.connect(server=('serveradress.com', portnumber))

修改后,我无法再现AttributeError错误。

我还建议您使用正确的JID来测试您的代码。JID类似于由@分隔的电子邮件地址,这就是为什么在您的示例代码中jid.getNode()返回空值的原因。

*我的代码示例更正如下:

import xmpp

user = 'username@gmail.com'
password = 'password'

jid = xmpp.JID(user)

connection = xmpp.Client(jid.getDomain())
connection.connect(server=('talk.google.com', 5223))
connection.auth(jid.getNode(), password)
connection.sendInitPresence()

谢谢。我仍然遇到错误,您可以在我编辑后的问题中看到完整的回溯信息。抱歉,在问题中已经修复了它。实际上,我的代码中确实有一个真正的JID。 - rok
我将我的可运行代码添加到了我的答案中。希望能对你有所帮助。 - N3RO
@ N3RO。谢谢,它在我家里运行正常,但连接到域网络的计算机上无法运行。 - rok

3

在您的追踪回溯中,看起来您正在尝试连接不存在的域名talk.gmail.com,因此connection.connect语句将无法打开连接。

尝试连接到talk.google.com,这可能是正确的服务器名称。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接