-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
219 lines (156 loc) · 4.9 KB
/
views.py
File metadata and controls
219 lines (156 loc) · 4.9 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os,json,threading
SESSION_STATUS = {0: 'NOT_INIT', 1: 'INIT', 2: 'LOGIN_SUCCESS', 3: 'TEMPORARY_LOGIN_FAILED',
4: 'FAILED_AND_QUIT', 5: 'TIME_OUT', 6: 'UNKNOWN_ERROR',7:'NEED_SLEEP'}
ELECTOR_STATUS = {0:'NOT_INIT',1:'INIT',2:'SUBMIT_SUCCEES',3:'SUBMIT_FAILED',4:'UNKNOWN_ERROR',5:'CONFLICT'}
class MainStatus: #This class is created only for demo. It shows that you can use the way we work in python to work with this http server.
def __init__(self):
self.id=1
self.article_list={}
self.security_code=51033218
def insert_article(self,data):
self.article_list[self.id]=data
self.id+=1
return self.id-1
def get_article_by_id(self,id):
try:
return self.article_list[id]
except:
return -1
def reinit(self,passphrase):
if passphrase==self.security_code:
self.id=1
self.article_list={}
return True
else:
return False
mainStatusMutex=threading.Lock()
mainStatus=MainStatus()
TEST_PAGE="""<!DOCTYPE html>
<html>
<head>
<link href="static/css/test.css" rel="stylesheet" />
</head>
<body>
<div class="test">
<p>灰化肥不会挥发</p>
<p>黑化肥挥发使灰化肥发黑</p>
</div>
<div>
<img src="/static/image/test.jpg" />
</div>
</body>
</html>
"""
DEFAULT_ERROR_MESSAGE = """\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code: %(code)d</p>
<p>Message: %(message)s.</p>
<p>Error code explanation: %(code)s - %(explain)s.</p>
</body>
</html>
"""
class FileOpeningError:
pass
class ViewsResponse:
def __init__(self, content, head={}, status=200):
self.status=status
self.head=head
self.content=content.encode("UTF-8")
if status==200:
self.head["Content-type"] = "text/html;charset=UTF-8"
if len(self.content)!=0:
self.head["Content-Length"]=str(len(self.content))
class ViewsAjaxResponse(ViewsResponse):
def __init__(self,dict):
ViewsResponse.__init__(self,json.dumps(dict),{})
class ViewsRedirect(ViewsResponse):
def __init__(self,url="/"):
ViewsResponse.__init__(self,"",head={"Location":url},status=301)
def open_file_as_string(filepath):
filepath=os.path.dirname(__file__)+filepath
if os.path.exists(filepath):
try:
f=open(filepath, "rb")
result = (f.read()).decode('utf-8','ignore')
f.close()
return result
except:
raise FileOpeningError
else:
raise FileNotFoundError
def command_selector(command,method,data):
if command=="/test":
return test(method,data)
elif command=="/post":
return post_article(method,data)
elif command=="/result":
return get_result(method,data)
elif command=="/clean":
return clean_records(method,data)
elif command=="/":
return index(method,data)
else:
return
### Demo of Views starts here:
def clean_records(method,data):#one dynamic page
try:
result=mainStatus.reinit(int(data['pass']))
if result:
return ViewsResponse("Success")
else:
return ViewsResponse("Incorrect passphrase",{},403)
except:
return ViewsResponse("",{},500)
def post_article(method,data):#one dynamic page
if method=="GET":
return ViewsResponse("",{},403)
elif method=="POST":
article=data['article']
id=mainStatus.insert_article(article)
if id:
return ViewsResponse(str(id))
else:
return ViewsResponse("",{},500)
else:
return ViewsResponse("",{},403)
def get_result(method,data):#one dynamic page
if method=="GET":
print(data)
article = ""
try:
with mainStatusMutex:
article=mainStatus.get_article_by_id(int(data['id']))
except:
return ViewsResponse("",{},404)
print(article)
try:
return ViewsResponse(str(len(article)))
except:
return ViewsResponse("",{},500)
else:
return ViewsResponse("",{},403)
def index(method,data):#one dynamic page
return ViewsRedirect("/test")
def test(method,data):#one dynamic page
if method=="POST":
for i in data:
print("key:"+i+"\n"+"value:"+data[i])
return ViewsResponse(TEST_PAGE)
elif method=="GET":
if not data:
return ViewsResponse(TEST_PAGE)
else:
c=""
for i in data:
c += i + ':' + data[i] + "\r\n"
return ViewsResponse(c)
else:
return ViewsRedirect('/css/test.css')