【问题标题】:Python2.6 xmpp Jabber ErrorPython2.6 xmpp Jabber错误
【发布时间】:2023-03-25 12:53:02
【问题描述】:

我在 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: 与服务器断开连接。

此外,当我运行其他语句时,我会在控制台中收到调试消息。

可能是什么问题,我该如何解决?

【问题讨论】:

  • 在您断开连接之前调试语句会说什么?您是否看到未经授权的错误?

标签: python xmpp


【解决方案1】:

我认为您需要在发送第一条消息之前致电sendInitPresence

...
cnx.auth(login,pwd, 'botty')
cnx.sendInitPresence()
cnx.send( xmpp.Message( "YourFriend@gmail.com" ,"Hello World form Python" ) )

【讨论】:

  • 试过了,但我仍然收到 IOError: Disconnected from server error
  • 不需要先发送出席信息。我刚刚针对 gtalk 进行了测试,以确保它们没有特殊规则。
【解决方案2】:

试试这个代码 sn-p。为简单起见,我没有处理错误条件。

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"


要关闭调试消息,请将 debug=[] 传递给 Client 类的构造函数的第二个参数:

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

【讨论】:

  • 试过 Philip 但我的身份验证失败。在尝试此之前是否需要更改任何设置?
  • 奇怪,我刚刚使用上面的代码 sn-p 再次尝试,使用有效的 gmail 用户名 (xxxxx@gmail.com) 和密码,它按预期发送了一条消息。这个(子集)代码每天在 Linux 和 Windows 机器上作为机器人运行。使用 Python 2.6 和 xmpppy-0.5.0rc1。也许确保登录包含@gmail.com 后缀?
  • 还要仔细检查您的密码。 :)
【解决方案3】:

Here 是它在my PyTalk client 上的表现。

不要忘记用户 ID 中的@gmail.com。

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

同时尝试为身份验证指定资源。

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

顺便说一句,它看起来离菲利普答案很近

【讨论】:

    【解决方案4】:

    我认为你必须写这个。我在 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"
    

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 1970-01-01
      • 2014-11-20
      • 2013-04-04
      • 2011-01-31
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2011-02-07
      相关资源
      最近更新 更多