-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSGI-Pyramid.py
84 lines (59 loc) · 2.26 KB
/
WSGI-Pyramid.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
import os
import sys
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.wsgi import wsgiapp
@wsgiapp
def index(environ, start_response):
status = '200 OK'
response_headers = [("Content-Type", "text/html")]
result = []
file = open('/Users/stiv/GitHub/Pyramid/index.html', 'rb')
for line in file:
result.append(line)
file.close()
start_response(status, response_headers)
return result
@wsgiapp
def aboutme(environ, start_response):
status = '200 OK'
response_headers = [("Content-Type", "text/html")]
result = []
file = open('/Users/stiv/GitHub/Pyramid/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
if __name__ == '__main__':
config = Configurator()
config.add_route('index1', '/index.html')
config.add_route('index2', '/')
config.add_route('aboutme', '/about/aboutme.html')
config.add_view(index, route_name='index1')
config.add_view(index, route_name='index2')
config.add_view(aboutme, route_name='aboutme')
pyramid_app = config.make_wsgi_app()
my_application = my_middleware(pyramid_app)
server = make_server('localhost', 8000, my_application)
server.serve_forever()