-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.py
executable file
·129 lines (89 loc) · 2.86 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/env python
#from wsgiref.util import setup_testing_defaults
# A relatively simple WSGI application. It's going to print out the
# environment dictionary after being updated by setup_testing_defaults
def spaceapi_app(environ, start_response):
#setup_testing_defaults(environ)
Router.set_environ(environ)
status = '200 OK'
headers = [('Content-type', 'text/plain; charset=utf-8')]
start_response(status, headers)
return {
'sensor' : SensorController.execute
}.get(Router.get_controller(), StatusController.execute)()
#request_uri = environ['REQUEST_URI']
#print(test)
#print('test')
#print >> environ['wsgi.errors'], "application debug #2"
#ret = request_uri
#ret = [("%s: %s\n" % (key, value)).encode("utf-8")
# for key, value in environ.items()]
return ret
class SensorController:
@classmethod
def execute(cls):
#return Router.get_action()
return {
'set' : cls.setAction
}.get(Router.get_action(), cls.noneAction)()
@classmethod
def noneAction(cls):
return ''
@classmethod
def setAction(cls):
return 'SensorController::set'
class StatusController:
@classmethod
def execute(cls):
return {
'json' : cls.jsonAction
}.get(Router.get_action(), cls.htmlAction)()
@classmethod
def jsonAction(cls):
return 'StatusController::json'
@classmethod
def htmlAction(cls):
return 'StatusController::html'
class Router:
environ = None
@classmethod
def set_environ(cls, environ):
cls.environ = environ
@classmethod
def get_segment(cls, index):
request_uri = cls.environ['REQUEST_URI']
segments = request_uri.strip('/').split('/')
if len(segments) >= index-1:
return segments[index]
else:
return ''
@classmethod
def get_controller(cls):
return cls.get_segment(0)
@classmethod
def get_action(cls):
return cls.get_segment(1)
class Output():
@staticmethod
def html():
return ''
@staticmethod
def json():
return ''
class Sensor():
@staticmethod
def save(sensors):
return ''
######################################################################
## use this line to use the script with mod_wsgi or
## other wsgi-compliant web servers/modules
application = spaceapi_app
## use the following lines to run the script as built-in web server
#from wsgiref.simple_server import make_server
#httpd = make_server('', 8052, spaceapi_app)
#print("Serving on port 8052...")
#httpd.serve_forever()
######################################################################
# http://docs.python.org/3.3/library/wsgiref.html
# https://wiki.archlinux.org/index.php/Mod_wsgi
# https://code.google.com/p/modwsgi/wiki/DebuggingTechniques