forked from xmpppy/xmpppy
-
Notifications
You must be signed in to change notification settings - Fork 13
Basic documentation
CyrilPeponnet edited this page Sep 23, 2014
·
2 revisions
This documents topic is for people who want to quickly try xmpppy for a simple task, like writing a command-line script for sending a single message.
This example demonstrates a simple script that sends message to one recipient.
Example: xsend [email protected] Hello there!
#!/usr/bin/python
import sys,os,xmpp
if len(sys.argv) < 2:
print "Syntax: xsend JID text"
sys.exit(0)
tojid=sys.argv[1]
text=' '.join(sys.argv[2:])
jidparams={}
if os.access(os.environ['HOME']+'/.xsend',os.R_OK):
for ln in open(os.environ['HOME']+'/.xsend').readlines():
key,val=ln.strip().split('=',1)
jidparams[key.lower()]=val
for mandatory in ['jid','password']:
if mandatory not in jidparams.keys():
open(os.environ['HOME']+'/.xsend','w').write('#[email protected]\n#PASSWORD=juliet\n')
print 'Please ensure the ~/.xsend file has valid JID for sending messages.'
sys.exit(0)
From now on we have to:
- connect to jabber server
- authenticate ourselves
- submit a message
jid=xmpp.protocol.JID(jidparams['jid'])
cl=xmpp.Client(jid.getDomain(),debug=[])
cl.connect()
cl.auth(jid.getNode(),jidparams['password'])
#cl.sendInitialPresence()
cl.send(xmpp.protocol.Message(tojid,text))
We're done! The session must now be closed but since we have not registered disconnect handler we will just leave it to python and TCP/IP layer. All jabber servers that I know handle such disconnects correctly.
You can see the final script from here xsend.py