-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSGI-Server.py
71 lines (44 loc) · 1.84 KB
/
WSGI-Server.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
import os
import sys
def my_application(environ, start_response):
status = '200 OK'
response_headers = [("Content-Type", "text/html"),
("Connection", "close")]
result = []
if environ["PATH_INFO"] == "/" or environ["PATH_INFO"] == "/index.html":
file = open('/Users/stiv/GitHub/myproject/index.html', 'rb')
for line in file:
result.append(line)
file.close()
if environ["PATH_INFO"] == "/about/aboutme.html":
file = open('/Users/stiv/GitHub/myproject/about/aboutme.html', 'rb')
for line in file:
result.append(line)
file.close()
start_response(status, response_headers)
return result
class my_middleware(object):
def __init__(self, my_application):
self.app = my_application
def __call__(self, environ, start_response):
top_position = -1
top_str = "\t\t<div class='top'>Middleware TOP</div>\n".encode()
bottom_position = -1
bottom_str = "\t\t<div class='botton'>Middleware BOTTOM</div>\n".encode()
response = self.app(environ, start_response)
for line in response:
string = line.decode()
if "<body>" in string :
top_position = response.index(line)
if "</body>" in string :
bottom_position = response.index(line)
response = response[:top_position+1] + [top_str] + \
response[top_position+1: bottom_position] + [bottom_str] +\
response[bottom_position:]
return response
my_application = my_middleware(my_application)
if __name__ == '__main__':
from paste import reloader
from paste.httpserver import serve
reloader.install()
serve(my_application, host='localhost', port=8000)