annotate makeitso/python_package/{{package}}/web.py @ 220:221bb5cea60d

one more time
author Jeff Hammel <k0scist@gmail.com>
date Mon, 14 Dec 2015 13:34:59 -0800
parents 745c785b6986
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
83
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 web handler for {{project}}
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
217
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
7 # imports
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
8 import argparse
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
9 import sys
118
5964dabba04a correct syntax error
Jeff Hammel <jhammel@mozilla.com>
parents: 83
diff changeset
10 from webob import Request, Response, exc
217
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
11 from wsgiref import simple_server
83
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 class Handler(object):
217
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
14 """WSGI HTTP Handler"""
83
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 def __init__(self, **kw):
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 pass
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 def __call__(self, environ, start_response):
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 request = Request(environ)
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 response = Response(content_type='text/plain',
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 body="{{project}}")
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 return response(environ, start_response)
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
217
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
25 def main(args=sys.argv[1:]):
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
26 """CLI"""
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
27
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
28 # parse command line
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
29 parser = argparse.ArgumentParser(description=__doc__)
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
30 parser.add_argument('-p', '--port', dest='port',
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
31 type=int, default=8080,
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
32 help="port to serve on")
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
33 options = parser.parse_arguments(args)
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
34
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
35 # instantiate WSGI handler
83
dc51bd3b8f6a add a sample web handler
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 app = Handler()
217
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
37
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
38 # serve it (Warning! Single threaded!)
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
39 server = simple_server.make_server(host='0.0.0.0',
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
40 port=options.port
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
41 app=app)
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
42 try:
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
43 server.serve_forever()
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
44 except KeyboardInterrupt:
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
45 pass
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
46
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
47 if __name__ == '__main__':
745c785b6986 spruce this up a bit
Jeff Hammel <k0scist@gmail.com>
parents: 118
diff changeset
48 main()