This repository was archived by the owner on Jan 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwsgi.py
99 lines (82 loc) · 2.76 KB
/
wsgi.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Yoan Blanc <[email protected]>"
import cgi
import locale
from datetime import datetime
from wsgiref.simple_server import make_server
from wsgi_accept_language import LangMiddleware
LOCALES = {
"en": "en_US.UTF8",
"fr": "fr_FR.UTF8",
"de": "de_DE.UTF8",
"it": "it_IT.UTF8",
}
LANGS = "en", "de", "fr", "it" # order matter
PORT = 8080
TEMPLATE = """
<!doctype HTML>
<html>
<meta charset=utf-8>
<title>WSGI Accept Language middleware Demo</title>
<style>
body{text-align:center;background:#222;color:#fff;}
#page{margin:1em auto;text-align:left;width:40em;padding:1em;}
pre {background:#ffe;border:2px solid #ffc;color:#000;padding:1em;}
</style>
<div id=page>
<h1>WSGI Accept Language middleware Demo</h1>
<form method="GET">
<label for=lang>Select language:</label>
<select name=lang id=lang>
<option value=en%(opt.en)s>English</option>
<option value=fr%(opt.fr)s>Français</option>
<option value=de%(opt.de)s>Deutsch</option>
<option value=it%(opt.it)s>Italiano</option>
</select>
<input type=submit value="Send">
</form>
<p>Now: %(now)s</p>
<pre><code>%(code)s</code></pre>
<address>By: %(by)s</address>
</div>
</html>"""
def application(environ, start_response):
# primitive QS parsing, use WebOb instead
qs = environ["QUERY_STRING"].split("&")
GET = {}
for item in qs:
if item.find("=") > -1:
key, value = item.split("=", 1)
GET[key] = value
if "lang" in GET:
environ["lang"] = GET["lang"]
try:
locale.setlocale(locale.LC_TIME, LOCALES.get(environ["lang"], "C"))
except locale.Error, e:
print "Unsupported locale: %s" % LOCALES.get(environ["lang"])
# source code
fp = open(__file__)
code = fp.read()
fp.close()
body = TEMPLATE % {
"now": datetime.utcnow().strftime("%A %d %B %Y"),
"opt.en": " selected" if environ["lang"] == "en" else "",
"opt.fr": " selected" if environ["lang"] == "fr" else "",
"opt.de": " selected" if environ["lang"] == "de" else "",
"opt.it": " selected" if environ["lang"] == "it" else "",
"code": cgi.escape(code),
"by": cgi.escape(__author__)
}
start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")])
return [body]
application = LangMiddleware(application,
LANGS,
with_cookie=True)
if __name__ == "__main__":
httpd = make_server('', PORT, application)
print "Serving on port %d…" % PORT
try:
httpd.serve_forever()
except KeyboardInterrupt, ki:
print "…Quitting."