author | Tero Marttila <terom@fixme.fi> |
Mon, 25 Jan 2010 19:46:25 +0200 | |
changeset 94 | a5e431cfcc4b |
permissions | -rw-r--r-- |
94
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
A tornado-based HTTP app |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
import tornado.web |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
import tornado.httpserver |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
import tornado.wsgi |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
import werkzeug |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
from pngtile import handlers |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
class MainHandler (tornado.web.RequestHandler) : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
Main handler for the / URL, pass off requests to werkzeug-based handlers... |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
def build_environ (self, path) : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
Yield a series of (key, value) pairs suitable for use with WSGI |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
request = self.request |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
hostport = request.host.split(":") |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
if len(hostport) == 2: |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
host = hostport[0] |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
port = int(hostport[1]) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
else: |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
host = request.host |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
port = 443 if request.protocol == "https" else 80 |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
33 |
yield "REQUEST_METHOD", request.method |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
yield "SCRIPT_NAME", "" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
yield "PATH_INFO", path |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
yield "QUERY_STRING", request.query |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
yield "SERVER_NAME", host |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
yield "SERVER_PORT", port |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
yield "wsgi.version", (1, 0) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
yield "wsgi.url_scheme", request.protocol |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
yield "CONTENT_TYPE", request.headers.get("Content-Type") |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
yield "CONTENT_LENGTH", request.headers.get("Content-Length") |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
for key, value in request.headers.iteritems(): |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
yield "HTTP_" + key.replace("-", "_").upper(), value |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
def get (self, path) : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
environ = dict(self.build_environ(path)) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
# build Request |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
request = werkzeug.Request(environ) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
# handle |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
try : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
response = handlers.handle_req(request) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
except werkzeug.exceptions.HTTPException, ex : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
response = ex |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
# return |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
def start_response (_status, _headers) : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
status = int(_status.split()[0]) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
headers = _headers |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
self.set_status(status) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
for name, value in headers : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
self.set_header(name, value) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
# invoke Response |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
data = response(environ, start_response) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
# output data |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
for chunk in data : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
self.write(chunk) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
def build_app () : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
return tornado.web.Application([ |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
# static, from $CWD/static/ |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
(r"/static/(.*)", tornado.web.StaticFileHandler, dict(path = "static/")), |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
# dir listings, image html, PNG tiles |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
(r"(/.*)", MainHandler), |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
]) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
def build_httpserver (app, port) : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
server = tornado.httpserver.HTTPServer(app) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
server.listen(port) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
return server |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
def main (port=8000) : |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
Build the app, http server and run the main loop |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
""" |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
import logging |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
101 |
logging.basicConfig(level=logging.DEBUG) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
103 |
app = build_app() |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
server = build_httpserver(app, port) |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
tornado.ioloop.IOLoop.instance().start() |
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
|
a5e431cfcc4b
a simple Tornado frontend that uses the werkzeug handlers
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |