coding utf-8 Wsgi-приложение import sys import os import cgi import tr

 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
# -*- coding: utf-8 -*-
''' Wsgi-приложение. '''
import sys
import os
import cgi
import traceback
sys.path.insert(0, os.path.dirname(__file__))
#~ from cfg import *
#~ import tpls.base
def application(environ, start_response):
DEBUG = True if environ['HTTP_HOST'].split(':')[0] == 'localhost' else False
try:
# основная функция
ret = ''
# страницы
ret += '<p>страница: %s</p>' % environ['PATH_INFO']
ret += '<p>GET: %s</p>' % cgi.parse_qs(environ['QUERY_STRING'])
ret += '<h2>environ</h2>'
ret += '<table>'
for k, v in environ.items():
ret += '<tr><td>%s</td><td>%s</td></tr>' % (k, v)
ret += '</table>'
# шаблонизируем
#~ ret = tpls.base.t % {'title':'Привет :)', 'body':ret}
# возвращаем результат
response_headers = [
('Content-type', 'text/html; charset=utf-8'),
('Content-Length', str(len(ret))),
]
start_response('200 OK', response_headers)
return [ret]
except Http404:
response_headers = [("content-type", "text/plain")]
start_response('404 Not Found', response_headers, sys.exc_info())
return ["Page not found"]
except:
response_headers = [("content-type", "text/plain")]
start_response('500 Error', response_headers, sys.exc_info())
err_msg = "\n=====\n%s \n=====\n\n%s" % (environ['HTTP_HOST'],
''.join(traceback.format_exception(*sys.exc_info())).strip())
if not DEBUG:
err_msg = "500 Error"
return [err_msg]
class Http404(Exception):
pass
class Http500(Exception):
pass