Python2.6 xmpp Jabber错误

4
我正在使用Python的XMPP,并希望创建一个简单的客户端与Gmail ID进行通信。
#!/usr/bin/python
import xmpp

login = 'Your.Login' # @gmail.com 
pwd   = 'YourPassword'

cnx = xmpp.Client('gmail.com')
cnx.connect( server=('talk.google.com',5223) )
cnx.auth(login,pwd, 'botty')

cnx.send( xmpp.Message( "YourFriend@gmail.com" ,"Hello World form Python" ) )

当我运行最后一行代码时,出现了异常。

IOError: 与服务器断开连接。

另外,在运行其他语句时,控制台中会输出调试信息。

可能的问题是什么?如何解决?


你在断开连接之前调试语句显示了什么?你看到了未经授权的错误吗?<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"> <not-authorized/> </failure> - Joe Hildebrand
4个回答

6

这里是它在我的PyTalk客户端上的表现。

不要忘记在用户ID中加上@gmail.com。

我认为你应该尝试在5222端口上连接talk.google.com。

还要尝试为auth指定一个资源。

import xmpp
import sys

userID   = 'Your.Login@gmail.com' 
password = 'YourPassword'
ressource = 'Script'

jid  = xmpp.protocol.JID(userID)
jabber     = xmpp.Client(jid.getDomain(), debug=[])

connection = jabber.connect(('talk.google.com',5222))
if not connection:
    sys.stderr.write('Could not connect\n')
else:
    sys.stderr.write('Connected with %s\n' % connection)

auth = jabber.auth(jid.getNode(), password, ressource)
if not auth:
    sys.stderr.write("Could not authenticate\n")
else:
    sys.stderr.write('Authenticate using %s\n' % auth)

jabber.sendInitPresence(requestRoster=1)
jabber.send(xmpp.Message( "YourFriend@gmail.com" ,"Hello World form Python" ))

顺便提一下,Philip Answer看起来非常接近。

1

尝试使用这段代码片段。出于简单起见,我没有处理错误条件。

import xmpp

login = 'Your.Login' # @gmail.com 
pwd   = 'YourPassword'

jid = xmpp.protocol.JID(login)
cl  = xmpp.Client(jid.getDomain(), debug=[])
if cl.connect(('talk.google.com',5223)):
    print "Connected"
else:
    print "Connectioned failed"

if cl.auth(jid.getNode(), pwd):
    cl.sendInitPresence()
    cl.send(xmpp.Message( "YourFriend@gmail.com" ,"Hello World form Python" ))
else:
    print "Authentication failed"


为了关闭调试信息,请在Client类的构造函数的第二个参数中传递debug=[]

cl  = xmpp.Client(jid.getDomain(), debug=[])

尝试过了,Philip,但我收到了身份验证失败的消息。在尝试此操作之前是否需要更改任何设置? - crashekar
1
奇怪,我刚刚再次尝试了上面的代码片段,使用有效的gmail用户名(xxxxx@gmail.com)和密码发送了一条预期的消息。这个(子集)代码每天在Linux和Windows机器上作为机器人运行。使用Python 2.6和xmpppy-0.5.0rc1。也许要确保登录包含@gmail.com后缀? - Philip Fourie
还要再次确认您的密码。 :) - Joe Hildebrand

1

我认为你必须写这个。我在Python 2.7中使用xmpppy 0.5.0rc1进行了测试,效果非常好:P :) :

import xmpp

login = 'your mail@gmail.com' # @gmail.com 
pwd   = 'your pass'
text='Hello worlD!'
tojid='your friend @gmail.com'



jid = xmpp.protocol.JID(login)
cl  = xmpp.Client(jid.getDomain(), debug=[])
if cl.connect(('talk.google.com',5223)):
    print "Connected"

else:
    print "Connectioned failed"

if cl.auth(jid.getNode(), pwd):
    cl.sendInitPresence()
    cl.send(xmpp.protocol.Message(tojid,text))
else:
    print "Authentication failed"

0
我认为在发送第一条消息之前,您需要调用sendInitPresence
...
cnx.auth(login,pwd, 'botty')
cnx.sendInitPresence()
cnx.send( xmpp.Message( "YourFriend@gmail.com" ,"Hello World form Python" ) )

尝试过了,但我仍然收到 IOError: 与服务器断开连接的错误。 - crashekar
发送在线状态并不是必须的。我刚刚测试了一下gtalk,确保他们没有特殊规定。 - Joe Hildebrand

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