summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMike Taylor <bear42@gmail.com>2015-04-28 22:44:27 -0400
committerMike Taylor <bear42@gmail.com>2015-04-28 22:44:27 -0400
commit192b7e0349429839261eb4d1d42d8f75933dc0c2 (patch)
treeecb5905d19a563b4e6fe864afd0122321bf7cea1 /examples
parent842157a6cc25d9e85e6e31b3cf3349ba83ece101 (diff)
parent80b60fc0483b21c8d5829b61cabbf9b46aa2c2fb (diff)
downloadslixmpp-192b7e0349429839261eb4d1d42d8f75933dc0c2.tar.gz
slixmpp-192b7e0349429839261eb4d1d42d8f75933dc0c2.tar.bz2
slixmpp-192b7e0349429839261eb4d1d42d8f75933dc0c2.tar.xz
slixmpp-192b7e0349429839261eb4d1d42d8f75933dc0c2.zip
Merge pull request #345 from sangeeths/xep_0332
XEP-0332: HTTP over XMPP transport
Diffstat (limited to 'examples')
-rw-r--r--examples/http_over_xmpp.py101
1 files changed, 101 insertions, 0 deletions
diff --git a/examples/http_over_xmpp.py b/examples/http_over_xmpp.py
new file mode 100644
index 00000000..a2fbf664
--- /dev/null
+++ b/examples/http_over_xmpp.py
@@ -0,0 +1,101 @@
+#!/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
+
+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()
+
+
+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 <jid> -P <pwd> -i <ip> -p <port> [-v]
+ #
+
+ 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')
+
+ 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....'
+