comparison example/persona_example.py @ 47:f2474ffcee96

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