-
Notifications
You must be signed in to change notification settings - Fork 99
/
example_webapp.py
53 lines (42 loc) · 1.64 KB
/
example_webapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#
# This is a picoweb example showing a centralized web page route
# specification (classical Django style).
#
import ure as re
import picoweb
def index(req, resp):
# You can construct an HTTP response completely yourself, having
# a full control of headers sent...
yield from resp.awrite("HTTP/1.0 200 OK\r\n")
yield from resp.awrite("Content-Type: text/html\r\n")
yield from resp.awrite("\r\n")
yield from resp.awrite("I can show you a table of <a href='squares'>squares</a>.<br/>")
yield from resp.awrite("Or my <a href='file'>source</a>.")
def squares(req, resp):
# Or can use a convenience function start_response() (see its source for
# extra params it takes).
yield from picoweb.start_response(resp)
yield from app.render_template(resp, "squares.tpl", (req,))
def hello(req, resp):
yield from picoweb.start_response(resp)
# Here's how you extract matched groups from a regex URI match
yield from resp.awrite("Hello " + req.url_match.group(1))
ROUTES = [
# You can specify exact URI string matches...
("/", index),
("/squares", squares),
("/file", lambda req, resp: (yield from app.sendfile(resp, "example_webapp.py"))),
# ... or match using a regex, the match result available as req.url_match
# for match group extraction in your view.
(re.compile("^/iam/(.+)"), hello),
]
import ulogging as logging
logging.basicConfig(level=logging.INFO)
#logging.basicConfig(level=logging.DEBUG)
app = picoweb.WebApp(__name__, ROUTES)
# debug values:
# -1 disable all logging
# 0 (False) normal logging: requests and errors
# 1 (True) debug logging
# 2 extra debug logging
app.run(debug=1)