From 9bef4b4d4d54768cb753e4bbfec049e97d063882 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Wed, 1 Sep 2010 14:47:42 -0400 Subject: Move the examples to a top-level examples directory. --- examples/component.py | 41 +++++++++++++++++++++++++++++++++++++ examples/echo_client.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/component.py create mode 100644 examples/echo_client.py (limited to 'examples') diff --git a/examples/component.py b/examples/component.py new file mode 100644 index 00000000..f24216c2 --- /dev/null +++ b/examples/component.py @@ -0,0 +1,41 @@ +import sleekxmpp.componentxmpp +import logging +from optparse import OptionParser +import time + +class Example(sleekxmpp.componentxmpp.ComponentXMPP): + + def __init__(self, jid, password): + sleekxmpp.componentxmpp.ComponentXMPP.__init__(self, jid, password, 'vm1', 5230) + self.add_event_handler("session_start", self.start) + self.add_event_handler("message", self.message) + + def start(self, event): + #self.getRoster() + #self.sendPresence(pto='admin@tigase.netflint.net/sarkozy') + #self.sendPresence(pto='tigase.netflint.net') + pass + + def message(self, event): + self.sendMessage("%s/%s" % (event['jid'], event['resource']), "Thanks for sending me, \"%s\"." % event['message'], mtype=event['type']) + +if __name__ == '__main__': + #parse command line arguements + optp = OptionParser() + optp.add_option('-q','--quiet', help='set logging to ERROR', action='store_const', dest='loglevel', const=logging.ERROR, default=logging.INFO) + optp.add_option('-d','--debug', help='set logging to DEBUG', action='store_const', dest='loglevel', const=logging.DEBUG, default=logging.INFO) + optp.add_option('-v','--verbose', help='set logging to COMM', action='store_const', dest='loglevel', const=5, default=logging.INFO) + optp.add_option("-c","--config", dest="configfile", default="config.xml", help="set config file to use") + opts,args = optp.parse_args() + + logging.basicConfig(level=opts.loglevel, format='%(levelname)-8s %(message)s') + xmpp = Example('component.vm1', 'secreteating') + xmpp.registerPlugin('xep_0004') + xmpp.registerPlugin('xep_0030') + xmpp.registerPlugin('xep_0060') + xmpp.registerPlugin('xep_0199') + if xmpp.connect(): + xmpp.process(threaded=False) + print("done") + else: + print("Unable to connect.") diff --git a/examples/echo_client.py b/examples/echo_client.py new file mode 100644 index 00000000..5e6314f0 --- /dev/null +++ b/examples/echo_client.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# coding=utf8 + +import sleekxmpp +import logging +from optparse import OptionParser +import time + +import sys + +if sys.version_info < (3,0): + reload(sys) + sys.setdefaultencoding('utf8') + + +class Example(sleekxmpp.ClientXMPP): + + def __init__(self, jid, password): + sleekxmpp.ClientXMPP.__init__(self, jid, password) + self.add_event_handler("session_start", self.start) + self.add_event_handler("message", self.message) + + def start(self, event): + self.getRoster() + self.sendPresence() + + def message(self, msg): + msg.reply("Thanks for sending\n%(body)s" % msg).send() + +if __name__ == '__main__': + #parse command line arguements + optp = OptionParser() + optp.add_option('-q','--quiet', help='set logging to ERROR', action='store_const', dest='loglevel', const=logging.ERROR, default=logging.INFO) + optp.add_option('-d','--debug', help='set logging to DEBUG', action='store_const', dest='loglevel', const=logging.DEBUG, default=logging.INFO) + optp.add_option('-v','--verbose', help='set logging to COMM', action='store_const', dest='loglevel', const=5, default=logging.INFO) + optp.add_option("-j","--jid", dest="jid", help="JID to use") + optp.add_option("-p","--password", dest="password", help="password to use") + opts,args = optp.parse_args() + + logging.basicConfig(level=opts.loglevel, format='%(levelname)-8s %(message)s') + xmpp = Example(opts.jid, opts.password) + xmpp.registerPlugin('xep_0030') + xmpp.registerPlugin('xep_0004') + xmpp.registerPlugin('xep_0060') + xmpp.registerPlugin('xep_0199') + + # use this if you don't have pydns, and want to + # talk to GoogleTalk (e.g.) + if xmpp.connect(('talk.google.com', 5222)): + #khif xmpp.connect(): + xmpp.process(threaded=False) + print("done") + else: + print("Unable to connect.") -- cgit v1.2.3