comparison makeitso/python_package/{{package}}/web.py @ 217:745c785b6986

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