-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.py
73 lines (54 loc) · 2.12 KB
/
portal.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
#!/usr/bin/env python
__author__ = 'bing'
import cgi
import cgitb
import os
from http.httprequest import HttpRequest
from http.httpreponse import HttpResponse
from confparser import ConfParser
cgitb.enable(display=0, logdir=".", format="text") # for troubleshooting
def load_class(class_name):
# calss_name must be fully qualified class name.
last_dot = class_name.rfind(".")
module_name = class_name[0:last_dot]
clsname = class_name[last_dot+1:]
mod = __import__(module_name, fromlist=[clsname])
cls = getattr(mod, class_name[last_dot+1:])
return cls
class Portal:
def __init__(self):
pass
def create_http_request(self):
"""Create Http Request from os environment variables."""
http_request = HttpRequest()
if "REQUEST_METHOD" in os.environ:
http_request.method = os.environ["REQUEST_METHOD"].strip().lower()
if "HTTP_COOKIE" in os.environ:
http_request.cookie = os.environ["HTTP_COOKIE"].strip()
if "QUERY_STRING" in os.environ:
http_request.query_string = os.environ["QUERY_STRING"].strip()
if "HTTP_ACCEPT" in os.environ:
http_request.accept = os.environ["HTTP_ACCEPT"].strip()
if "REQUEST_URI" in os.environ:
http_request.request_uri = os.environ["REQUEST_URI"].strip()
return http_request
def handle_request(self):
http_request = self.create_http_request()
http_response = HttpResponse()
field_storage = cgi.FieldStorage()
action = field_storage.getfirst("action").strip()
confparser = ConfParser()
handler_section = confparser.read_section(section="handler")
try:
handler_class_name = handler_section[action]
except KeyError:
handler_class_name = "view.handler.ErrorPageHandler"
handler_class = load_class(handler_class_name)
handler = handler_class()
handler.handle(http_request, http_response)
print(http_response)
#print("Content:text/html\n\n")
#for i in os.environ:
# print '%s => %s' % (i, os.environ[i])
portal = Portal()
portal.handle_request()