Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python2.6 xmpp Jabber Error

Tags:

python

xmpp

I am using xmpp with python and I want create a simple client to communicate with a 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( "[email protected]" ,"Hello World form Python" ) )

When I run the last line I get an exception

IOError: Disconnected from server.

Also when I run the other statements I get debug messages in the console.

What could be the issue and how can I resolve it ?

like image 905
crashekar Avatar asked Mar 01 '23 09:03

crashekar


2 Answers

Here is how it did on my PyTalk client.

Don't forget the @gmail.com in the userID.

I think you should try to connect talk.google.com on the 5222 port.

Also try to specify a ressource for the auth.

import xmpp
import sys

userID   = '[email protected]' 
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( "[email protected]" ,"Hello World form Python" ))

By the way, it looks very close from Philip Answer

like image 99
2 revs Avatar answered Mar 05 '23 14:03

2 revs


Try this code snippet. I didn't handle the error conditions for simplicity's sake.

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( "[email protected]" ,"Hello World form Python" ))
else:
    print "Authentication failed"


To switch off the debugging messages, pass debug=[] for the 2nd parameter on the Client class's constructor:

cl  = xmpp.Client(jid.getDomain(), debug=[])
like image 38
Philip Fourie Avatar answered Mar 05 '23 13:03

Philip Fourie