view example/persona.py @ 30:0bf52646061b

example/persona.html example/persona.py setup.py
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 27 Dec 2013 11:24:23 -0800
parents b411066b4929
children 6af220013aa1
line wrap: on
line source

#!/usr/bin/env python

"""
test Mozilla persona auth

https://login.persona.org/about
"""

import optparse
import os
import sys
from wsgiref import simple_server

here = os.path.dirname(os.path.realpath(__file__))
page = os.path.join(here, 'persona.html')

class PersonaTest(object):
    def __init__(self, page=page):
        self.page = page
        assert os.path.exists(self.page), "File '%s' not found" % self.page
        self.page = open(self.page, 'r').read()
    def __call__(self, environ, start_response):
        method = environ['REQUEST_METHOD']
        if method == 'GET':
            start_response("200 OK", [('Content-Type', 'text/html'),
                                      ('Content-Length', str(len(self.page)))])
            body = self.page
        elif method == 'POST':
            body = '\n'.join([])
        return [body]

def main(args=sys.argv[1:]):

    usage = '%prog [options]'
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    parser.add_option('-p', '--port', dest='port',
                      type='int', default=8123,
                      help="port to serve on")
    options, args = parser.parse_args(args)

    app = PersonaTest()

    server = simple_server.make_server('127.0.0.1', options.port, app)
    print 'server on\nhttp://localhost:%d/' % options.port
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    main()