From 8afba7de853be4eb3c5cb2467fa17db64cf84abc Mon Sep 17 00:00:00 2001 From: Sangeeth Saravanaraj Date: Thu, 22 Jan 2015 16:38:16 +0530 Subject: renamed example for convenience. --- examples/http_over_xmpp.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 examples/http_over_xmpp.py (limited to 'examples/http_over_xmpp.py') diff --git a/examples/http_over_xmpp.py b/examples/http_over_xmpp.py new file mode 100644 index 00000000..5de8713e --- /dev/null +++ b/examples/http_over_xmpp.py @@ -0,0 +1,70 @@ +from sleekxmpp import ClientXMPP +import logging + + +# def tracefunc(frame, event, arg, indent=[0]): +# prefix = "/Users/sangeeth/code/SleekXMPP/sleekxmpp/" +# if not frame.f_code.co_filename.startswith(prefix): +# return tracefunc +# if event == "call": +# indent[0] += 2 +# cn = getattr( +# getattr(frame.f_locals.get("self"), "__class__", None), +# "__name__", None +# ) +# print "{}{} {} {}".format( +# "." * indent[0], frame.f_code.co_filename[len(prefix):], +# cn, frame.f_code.co_name +# ) +# elif event == "return": +# indent[0] -= 2 +# return tracefunc +# +# +# import sys +# sys.settrace(tracefunc) + + +class HTTPOverXMPPClient(ClientXMPP): + def __init__(self, jid, password): + ClientXMPP.__init__(self, jid, password) + + self.register_plugin('xep_0332') # HTTP over XMPP + # self.register_plugin('xep_0030') # Service Discovery + # self.register_plugin('xep_0004') # Data Forms + # self.register_plugin('xep_0060') # PubSub + # self.register_plugin('xep_0199') # XMPP Ping + # + # self.add_event_handler("session_start", self.session_start) + # self.add_event_handler("connected", self.connected) + # + # def session_start(self, event): + # print "Client::session_start()" + # self.send_presence() + # print self.get_roster() + # + # def connected(self, event): + # print "Client::connected()" + + +def get_cred(filename="/tmp/.cred"): + with open(filename, "r") as f: + return f.readline().split() + + +if __name__ == "__main__": + + logging.basicConfig( + level=logging.DEBUG, format='%(levelname)-8s %(message)s' + ) + + jid, password = get_cred() + xmpp = HTTPOverXMPPClient(jid, password) + if xmpp.connect(("talk.l.google.com", 5222)): + print "Connected!" + xmpp.process(block=True) + else: + print "Not connected!" + print "Goodbye...." + + -- cgit v1.2.3 From 24264d3a07f8a1576637e6a5703682e8735c2be1 Mon Sep 17 00:00:00 2001 From: Sangeeth Saravanaraj Date: Thu, 5 Feb 2015 18:10:10 +0530 Subject: Updated Example.. --- examples/http_over_xmpp.py | 138 ++++++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 57 deletions(-) (limited to 'examples/http_over_xmpp.py') diff --git a/examples/http_over_xmpp.py b/examples/http_over_xmpp.py index 5de8713e..5630f778 100644 --- a/examples/http_over_xmpp.py +++ b/examples/http_over_xmpp.py @@ -1,70 +1,94 @@ -from sleekxmpp import ClientXMPP -import logging +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + SleekXMPP: The Sleek XMPP Library + Implementation of HTTP over XMPP transport + http://xmpp.org/extensions/xep-0332.html + Copyright (C) 2015 Riptide IO, sangeeth@riptideio.com + This file is part of SleekXMPP. + See the file LICENSE for copying permission. +""" + +from sleekxmpp import ClientXMPP -# def tracefunc(frame, event, arg, indent=[0]): -# prefix = "/Users/sangeeth/code/SleekXMPP/sleekxmpp/" -# if not frame.f_code.co_filename.startswith(prefix): -# return tracefunc -# if event == "call": -# indent[0] += 2 -# cn = getattr( -# getattr(frame.f_locals.get("self"), "__class__", None), -# "__name__", None -# ) -# print "{}{} {} {}".format( -# "." * indent[0], frame.f_code.co_filename[len(prefix):], -# cn, frame.f_code.co_name -# ) -# elif event == "return": -# indent[0] -= 2 -# return tracefunc -# -# -# import sys -# sys.settrace(tracefunc) +from optparse import OptionParser +import logging +import getpass class HTTPOverXMPPClient(ClientXMPP): def __init__(self, jid, password): ClientXMPP.__init__(self, jid, password) + self.register_plugin('xep_0332') # HTTP over XMPP Transport + self.add_event_handler( + 'session_start', self.session_start, threaded=True + ) + self.add_event_handler('http_request', self.http_request_received) + self.add_event_handler('http_response', self.http_response_received) + + def http_request_received(self, iq): + pass + + def http_response_received(self, iq): + print 'HTTP Response Received : ', iq + print 'From : ', iq['from'] + print 'To : ', iq['to'] + print 'Type : ', iq['type'] + print 'Headers : ', iq['resp']['headers'] + print 'Code : ', iq['resp']['code'] + print 'Message : ', iq['resp']['message'] + print 'Data : ', iq['resp']['data'] + + def session_start(self, event): + # TODO: Fill in the blanks + self['xep_0332'].send_request( + to='?', method='?', resource='?', headers={} + ) + self.disconnect() + - self.register_plugin('xep_0332') # HTTP over XMPP - # self.register_plugin('xep_0030') # Service Discovery - # self.register_plugin('xep_0004') # Data Forms - # self.register_plugin('xep_0060') # PubSub - # self.register_plugin('xep_0199') # XMPP Ping - # - # self.add_event_handler("session_start", self.session_start) - # self.add_event_handler("connected", self.connected) - # - # def session_start(self, event): - # print "Client::session_start()" - # self.send_presence() - # print self.get_roster() - # - # def connected(self, event): - # print "Client::connected()" - - -def get_cred(filename="/tmp/.cred"): - with open(filename, "r") as f: - return f.readline().split() - - -if __name__ == "__main__": - - logging.basicConfig( - level=logging.DEBUG, format='%(levelname)-8s %(message)s' +if __name__ == '__main__': + + parser = OptionParser() + + # Output verbosity options. + parser.add_option( + '-v', '--verbose', help='set logging to DEBUG', action='store_const', + dest='loglevel', const=logging.DEBUG, default=logging.ERROR + ) + + # JID and password options. + parser.add_option('-J', '--jid', dest='jid', help='JID') + parser.add_option('-P', '--password', dest='password', help='Password') + + # XMPP server ip and port options. + parser.add_option( + '-i', '--ipaddr', dest='ipaddr', + help='IP Address of the XMPP server', default=None ) + parser.add_option( + '-p', '--port', dest='port', + help='Port of the XMPP server', default=None + ) + + opts, args = parser.parse_args() + + # Setup logging. + logging.basicConfig(level=opts.loglevel, + format='%(levelname)-8s %(message)s') - jid, password = get_cred() - xmpp = HTTPOverXMPPClient(jid, password) - if xmpp.connect(("talk.l.google.com", 5222)): - print "Connected!" + if opts.jid is None: + opts.jid = raw_input('Username: ') + if opts.password is None: + opts.password = getpass.getpass('Password: ') + + xmpp = HTTPOverXMPPClient(opts.jid, opts.password) + if xmpp.connect((opts.ipaddr, int(opts.port))): + print 'Connected!' xmpp.process(block=True) else: - print "Not connected!" - print "Goodbye...." - + print 'Not connected!' + print 'Goodbye....' -- cgit v1.2.3 From 904480712157d762d35de16ce55202d641a56b3c Mon Sep 17 00:00:00 2001 From: Sangeeth Saravanaraj Date: Thu, 5 Feb 2015 18:11:41 +0530 Subject: Added help for running example.. --- examples/http_over_xmpp.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'examples/http_over_xmpp.py') diff --git a/examples/http_over_xmpp.py b/examples/http_over_xmpp.py index 5630f778..a2fbf664 100644 --- a/examples/http_over_xmpp.py +++ b/examples/http_over_xmpp.py @@ -51,6 +51,13 @@ class HTTPOverXMPPClient(ClientXMPP): if __name__ == '__main__': + # + # NOTE: To run this example, fill up the blanks in session_start() and + # use the following command. + # + # ./http_over_xmpp.py -J -P -i -p [-v] + # + parser = OptionParser() # Output verbosity options. -- cgit v1.2.3