views:

623

answers:

4

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 ?

A: 

I think you need to call sendInitPresence before sending the first message:

...
cnx.auth(login,pwd, 'botty')
cnx.sendInitPresence()
cnx.send( xmpp.Message( "[email protected]" ,"Hello World form Python" ) )
Nadia Alramli
Tried that but I still got the IOError: Disconnected from server error
crashekar
Sending presence first is not required. I just tested against gtalk to make sure they don't have a special rule.
Joe Hildebrand
A: 

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=[])
Philip Fourie
Tried that Philip but I get authentication failed. Is there any setting which needs to be changed before trying this ?
crashekar
Strange, I just tried it again with the code snippet above, used with a valid gmail username ([email protected]) and password and it sent a message as expected. This (sub-set) of code runs as a bot on Linux and Windows machines everyday. Python 2.6 and xmpppy-0.5.0rc1 used. Perhaps make sure the login contains the @gmail.com suffix?
Philip Fourie
And also double-check your password. :)
Joe Hildebrand
+1  A: 

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

Natim
A: 

i think you must write this. i test it in python 2.7 with xmpppy 0.5.0rc1 and work IT very nice :P :) :

import xmpp

login = 'your [email protected]' # @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"

iraj jelodari