slixmpp/examples/http_over_xmpp.py

93 lines
2.8 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
2015-02-05 12:40:10 +00:00
2021-02-05 19:51:15 +00:00
# Slixmpp: The Slick 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 slixmpp.
# See the file LICENSE for copying permission.
2015-02-05 12:40:10 +00:00
from slixmpp import ClientXMPP
2015-01-22 06:00:38 +00:00
from argparse import ArgumentParser
2015-02-05 12:40:10 +00:00
import logging
import getpass
2015-01-22 06:00:38 +00:00
class HTTPOverXMPPClient(ClientXMPP):
def __init__(self, jid, password):
ClientXMPP.__init__(self, jid, password)
2015-02-05 12:40:10 +00:00
self.register_plugin('xep_0332') # HTTP over XMPP Transport
self.add_event_handler(
2017-02-14 00:04:38 +00:00
'session_start', self.session_start
2015-02-05 12:40:10 +00:00
)
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 : %s' % iq)
print('From : %s' % iq['from'])
print('To : %s' % iq['to'])
print('Type : %s' % iq['type'])
print('Headers : %s' % iq['resp']['headers'])
print('Code : %s' % iq['resp']['code'])
print('Message : %s' % iq['resp']['message'])
print('Data : %s' % iq['resp']['data'])
2015-02-05 12:40:10 +00:00
def session_start(self, event):
# TODO: Fill in the blanks
self['xep_0332'].send_request(
to='?', method='?', resource='?', headers={}
)
self.disconnect()
2015-01-22 06:00:38 +00:00
2015-02-05 12:40:10 +00:00
if __name__ == '__main__':
2015-02-05 12:41:41 +00:00
#
# 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 = ArgumentParser()
2015-02-05 12:40:10 +00:00
# Output verbosity options.
parser.add_argument(
2015-02-05 12:40:10 +00:00
'-v', '--verbose', help='set logging to DEBUG', action='store_const',
dest='loglevel', const=logging.DEBUG, default=logging.ERROR
)
# JID and password options.
parser.add_argument('-J', '--jid', dest='jid', help='JID')
parser.add_argument('-P', '--password', dest='password', help='Password')
2015-02-05 12:40:10 +00:00
# XMPP server ip and port options.
parser.add_argument(
2015-02-05 12:40:10 +00:00
'-i', '--ipaddr', dest='ipaddr',
help='IP Address of the XMPP server', default=None
2015-01-22 06:00:38 +00:00
)
parser.add_argument(
2015-02-05 12:40:10 +00:00
'-p', '--port', dest='port',
help='Port of the XMPP server', default=None
)
args = parser.parse_args()
2015-02-05 12:40:10 +00:00
# Setup logging.
logging.basicConfig(level=args.loglevel,
2015-02-05 12:40:10 +00:00
format='%(levelname)-8s %(message)s')
2015-01-22 06:00:38 +00:00
if args.jid is None:
args.jid = input('Username: ')
if args.password is None:
args.password = getpass.getpass('Password: ')
2015-02-05 12:40:10 +00:00
xmpp = HTTPOverXMPPClient(args.jid, args.password)
xmpp.connect()
xmpp.process()