encoding utf-8 import simplejson def get_js environ start_response sta

 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
# encoding: utf-8
import simplejson
def get_js(environ, start_response):
start_response('200 Ok', [
('Content-Type', 'text/javascript')
])
return [file('ex.js').read()]
def get_json_data(environ, start_response):
start_response('200 Ok', [
('Content-Type', 'text/x-json; encoding=utf-8')
])
data = ['foo', 'bar', 'baz']
return [simplejson.dumps(data)]
def get_page(environ, start_response):
start_response('200 Ok', [
('Content-Type', 'text/html; encoding=utf-8')
])
return [file('ex.html').read()]
def mainapp(environ, start_response):
if environ['PATH_INFO'] == '/json':
print "Return json data"
return get_json_data(environ, start_response)
elif environ['PATH_INFO'] == '/ex.js':
print "Return js"
return get_js(environ, start_response)
else:
print "Regular request, show the page"
return get_page(environ, start_response)
if __name__ == '__main__':
from wsgiref import simple_server
httpd = simple_server.make_server('', 8000, mainapp)
print "Listening on http://127.0.0.1:8000/"
httpd.serve_forever()