-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
756 lines (721 loc) · 34.9 KB
/
application.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import os
import cgi
import json
import base64
import socket
import tempfile
from DBManager.Advertisement import upload_ad, get_ad_information, delete_ad_information, update_ad_information
from settings import ROOT_PATH
from utils.GetImageInformation import get_image_information
from utils.GetUrl import get_url_data
from DBManager.AuthToken import BasicAuth
from http.server import BaseHTTPRequestHandler
from DBManager.Permission import permission_status
from DBManager.RichTextInfo import upload_rich_text, get_rich_text, update_rich_text, delete_rich_text, \
delete_rich_text_image
from DBManager.UserInfo import UserRegist, UserLogin
from DBManager.ImageInfo import UploadImage, GetImage, GetImageList, DeleteImage
from DBManager.ManageInfo import ManageLogin, ManageRegist
from DBManager.PermissionManage import get_superuser_status, get_user_permission, manage_permission
from DBManager.TokenAuthorization import token_authorization
from utils.ResponseGoodMessage import normal_good_message, data_good_message
from utils.ResponseBadMessage import bad_message
from DBManager.WebInformation import upload_webinformation, get_web_information
from utils.ContentType import content_type
class Application(BaseHTTPRequestHandler):
# 用户民密码检验
def basic_auth(self):
try:
auth_header = self.headers.get('Authorization')
auth_token = auth_header.split(' ')[-1]
username_password = base64.b64decode(auth_token).decode('utf-8')
username, password = username_password.split(':')
basic_auth_status = BasicAuth(username, password)
return username, basic_auth_status
except Exception:
return "", 400
# Token验证
def token_auth(self):
try:
authorization_header = self.headers.get('Authorization')
token = authorization_header[1:-1]
username, status = token_authorization(token)
return username, status
except Exception:
return "", 400
# 身份验证
def auth(self):
username1, status1 = self.basic_auth()
username2, status2 = self.token_auth()
if status1 == 400 and status2 == 400:
return "", 400, bad_message("身份验证失败")
elif status1 == 200 and status2 == 400:
return username1, 200, normal_good_message("身份验证成功")
elif status1 == 400 and status2 == 200:
return username2, 200, normal_good_message("身份验证成功")
else:
if username1 == username2:
return username1, 200, normal_good_message("身份验证成功")
else:
return "", 400, bad_message("用户名与Token不匹配,身份验证失败")
# GET请求
def do_GET(self):
path = self.path.split("?")
# 主页
if path[0] == "/":
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write("home".encode("utf-8"))
# 获取照片
elif path[0] == "/api/image_info":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['read_permission'])):
try:
url = f"http://{self.headers['Host']}{self.path}"
imagename = get_url_data(url)['image']
response_code, message = GetImage(imagename)
for i in message:
with open(i, "rb") as f:
data = f.read()
if response_code == 200:
self.send_response(response_code)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
self.wfile.write(base64.b64encode(data))
else:
bmessage = message.encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
except Exception:
bmessage = "数据格式错误".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 图片信息
elif path[0] == "/api/image_list":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['read_permission'])):
try:
response_code, message = GetImageList()
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 查看所有用户权限
elif path[0] == "/api/manage_permission_list":
username, status, message = self.auth()
if status == 200:
if bool(get_superuser_status(username)):
try:
response_code, message = get_user_permission()
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "非管理员用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 富文本
elif path[0] == "/api/rich_text":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['read_permission'])):
try:
url = f"http://{self.headers['Host']}{self.path}"
rich_text_type = get_url_data(url)['type'][0]
response_code, message = get_rich_text(rich_text_type)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
elif path[0] == "/api/web_information":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['read_permission'])):
try:
response_code, message = get_web_information()
bmessage = message.encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
except Exception:
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write("数据格式错误".encode("utf-8"))
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
elif path[0] == "/api/ad_information":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['read_permission'])):
try:
url = f"http://{self.headers['Host']}{self.path}"
type_class = get_url_data(url)['type'][0]
response_code, message = get_ad_information(type_class)
bmessage = message.encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
except Exception:
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write("数据格式错误".encode("utf-8"))
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 无响应页
else:
dir_path = path[0].split(".")[0].split("/")
if dir_path[1] == "media":
contenttype = content_type(path[0].split(".")[1])
root_path = ROOT_PATH.replace("\\", "/")
static_path = "/".join(path[0].split("/")[:-1])
try:
with open(root_path + path[0], 'rb') as file:
data = file.read()
with tempfile.NamedTemporaryFile(delete=True, dir=root_path + static_path + "/") as temp_file:
temp_file.write(data)
temp_file.flush()
temp_file.seek(0)
file_dat = temp_file.read()
self.send_response(200)
self.send_header('Content-type', f"{contenttype}")
self.end_headers()
self.wfile.write(file_dat)
except Exception:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"404 NOT FOUND")
else:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"404 NOT FOUND")
# POST请求
def do_POST(self):
# 管理员注册
if self.path == "/api/manage_regist":
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
response_code, message = ManageRegist(data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 管理员登录
elif self.path == "/api/manage_login":
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
response_code, message = ManageLogin(data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 权限管理
elif self.path == "/api/manage_permission_list":
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
response_code, message = manage_permission(data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 普通用户注册
elif self.path == "/api/regist":
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
response_code, message = UserRegist(data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 普通用户登录
elif self.path == "/api/login":
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
response_code, message = UserLogin(data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 上传图片
elif self.path == "/api/image_info":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['upload_permission'])):
try:
content_type, _ = cgi.parse_header(self.headers['content-type'])
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
file_field = form['image']
if file_field == "":
response_code, message = UploadImage("", "", data)
else:
imagename, imagefile = get_image_information(file_field)
response_code, message = UploadImage(imagefile, imagename, data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 上传富文本
elif self.path == "/api/rich_text":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['upload_permission'])):
try:
content_type, _ = cgi.parse_header(self.headers['content-type'])
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
file_field = form['image']
data = eval(form["text"].value)
imagename, imagefile = get_image_information(file_field)
response_code, message = upload_rich_text(imagename, imagefile, data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 上传网站信息
elif self.path == "/api/web_information":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['upload_permission'])):
try:
content_type, _ = cgi.parse_header(self.headers['content-type'])
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
file_field = form['image']
data = eval(form["text"].value)
_, imagefile = get_image_information(file_field)
if file_field.value == "":
imagefile = [None]
response_code, message = upload_webinformation(imagefile, data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
elif self.path == "/api/ad_information":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['upload_permission'])):
try:
content_type, _ = cgi.parse_header(self.headers['content-type'])
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
file_field = form['image']
data = eval(form["text"].value)
imagename, imagefile = get_image_information(file_field)
if len(imagename) > 1:
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bad_message("数据格式错误"))
else:
response_code, message = upload_ad(imagename[0], imagefile[0], data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 无响应页
else:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"404 NOT FOUND")
# UPDATE请求
def do_PUT(self):
if self.path == "/api/rich_text":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['update_permission'])):
try:
content_type, _ = cgi.parse_header(self.headers['content-type'])
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
file_field = form['image']
data = eval(form["text"].value)
imagename, imagefile = get_image_information(file_field)
if file_field == "" or file_field == "null":
response_code, message = update_rich_text("", "", data)
else:
response_code, message = update_rich_text(imagename, imagefile, data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
elif self.path == "/api/ad_information":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['upload_permission'])):
try:
content_type, _ = cgi.parse_header(self.headers['content-type'])
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
file_field = form['image']
data = eval(form["text"].value)
if file_field.value == "" or file_field.value == "null":
response_code, message = update_ad_information("", "", data)
bmessage = message.encode("utf-8")
else:
imagename, imagefile = get_image_information(file_field)
if len(imagename) > 1:
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bad_message("数据格式错误"))
else:
response_code, message = update_ad_information(imagename[0], imagefile[0], data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 无响应页
else:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'404 Not Found')
def do_DELETE(self):
# 删除富文本
if self.path == "/api/rich_text":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['update_permission'])):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
key = list(data.keys())
if key[0] == "text_id":
response_code, message = delete_rich_text(data)
elif key[0] == "url":
response_code, message = delete_rich_text_image(data)
else:
response_code = 400
message = bad_message("数据错误")
bmessage = message.encode("utf-8")
# try:
#
# except Exception:
# response_code = 400
# bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
# 删除图片
elif self.path == "/api/image_info":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['update_permission'])):
try:
content_type, _ = cgi.parse_header(self.headers['content-type'])
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
image_name = form['image'].value
response_code, message = DeleteImage(image_name)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
elif self.path == "/api/ad_information":
username, status, message = self.auth()
if status == 200:
data = permission_status(username)
if bool(int(data['update_permission'])):
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
response_code, message = delete_ad_information(post_data)
bmessage = message.encode("utf-8")
except Exception:
response_code = 400
bmessage = "数据格式错误".encode("utf-8")
self.send_response(response_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = "用户缺少权限".encode("utf-8")
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
bmessage = message.encode("utf-8")
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bmessage)
else:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'404 Not Found')