forked from pfalcon/picoweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_extra_headers.py
51 lines (40 loc) · 1.32 KB
/
example_extra_headers.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
#
# This is a picoweb example showing the usage of
# extra headers in responses.
#
import picoweb
import ure as re
app = picoweb.WebApp(__name__)
# Shows sending extra headers specified as a dictionary.
@app.route("/")
def index(req, resp):
headers = {"X-MyHeader1": "foo", "X-MyHeader2": "bar"}
# Passing headers as a positional param is more efficient,
# but we pass by keyword here ;-)
yield from picoweb.start_response(resp, headers=headers)
yield from resp.awrite(b"""\
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet">
</head>
<body>
<p>The style.css should be cached and might be encoded.</p>
<p class="green">Check out your webdev tool!</p>
</body>
</html>""")
# Send gzipped content if supported by client.
# Shows specifying headers as a flat binary string -
# more efficient if such headers are static.
@app.route(re.compile('^\/(.+\.css)$'))
def styles(req, resp):
file_path = req.url_match.group(1)
headers = b"Cache-Control: max-age=86400\r\n"
if b"gzip" in req.headers.get(b"Accept-Encoding", b""):
file_path += ".gz"
headers += b"Content-Encoding: gzip\r\n"
print("sending " + file_path)
yield from app.sendfile(resp, "static/" + file_path, "text/css", headers)
import ulogging as logging
logging.basicConfig(level=logging.INFO)
app.run(debug=True)