comparison example/wsgibrowserid_example.py @ 39:8255e769828b

example/wsgibrowserid_example.py example/wsgibrowserid.py
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 27 Dec 2013 20:02:00 -0800
parents example/wsgibrowserid.py@4dfffa966edd
children
comparison
equal deleted inserted replaced
38:4dfffa966edd 39:8255e769828b
1 #!/usr/bin/env python
2
3 """
4 test Mozilla persona auth via wsgibrowserid
5
6 https://github.com/ianb/wsgibrowserid
7 """
8
9 import optparse
10 import os
11 import sys
12 from webob import Request, Response, exc
13 from wsgiref import simple_server
14
15 here = os.path.dirname(os.path.realpath(__file__))
16 page = os.path.join(here, 'wsgibrowserid.html')
17
18 class PersonaTest(object):
19 def __init__(self, page=page):
20 self.page = page
21 assert os.path.exists(self.page), "File '%s' not found" % self.page
22 self.page = open(self.page, 'r').read()
23 def __call__(self, environ, start_response):
24 request = Request(environ)
25 method = request.method
26 if method == 'GET':
27 content_type = 'text/html'
28 body = self.page
29 elif method == 'POST':
30 content_type = 'text/plain'
31 assertion = request.POST.get('assertion')
32 print ("Assertion: %s" % assertion)
33 body = '\n'.join(['%s: %s' % (key, environ[key])
34 for key in sorted(environ.keys())])
35 else:
36 content_type = 'text/plain'
37 body = 'Try GET or POST to do something interesting (How did you get here?)'
38 start_response("200 OK", [('Content-Type', content_type),
39 ('Content-Length', str(len(body)))])
40 return [body]
41
42 def main(args=sys.argv[1:]):
43
44 usage = '%prog [options]'
45 parser = optparse.OptionParser(usage=usage, description=__doc__)
46 parser.add_option('-p', '--port', dest='port',
47 type='int', default=8123,
48 help="port to serve on")
49 options, args = parser.parse_args(args)
50
51 app = PersonaTest()
52
53 server = simple_server.make_server('127.0.0.1', options.port, app)
54 print 'server on\nhttp://localhost:%d/' % options.port
55 try:
56 server.serve_forever()
57 except KeyboardInterrupt:
58 pass
59
60 if __name__ == '__main__':
61 main()