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
|
#!/usr/bin/env python3
import http.server
import os
import socket
import socketserver
import sys
import time
import dummy_killer
PORT = 18080
HOST_NAME = '0.0.0.0'
PID = "/tmp/dummy_http.pid"
class MyHandler(http.server.BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
def do_HEAD(self):
if self.path == "/redirect1":
self.send_response(301)
self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/hello")
elif self.path == "/redirect2":
self.send_response(301)
self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/redirect1")
elif self.path == "/redirect3":
self.send_response(301)
self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/redirect4")
elif self.path == "/redirect4":
self.send_response(301)
self.send_header("Location", "http://127.0.0.1:"+str(PORT)+"/redirect3")
else:
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.log_message("to be closed: " + repr(self.close_connection))
def do_GET(self):
"""Respond to a GET request."""
response = b"hello world"
if self.path == "/empty":
self.finish()
return
if self.path == "/timeout":
time.sleep(2)
if self.path == "/error_403":
self.send_response(403)
else:
self.send_response(200)
if self.path == "/content-length":
self.send_header("Content-Length", str(len(response)))
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(response)
self.log_message("to be closed: %d, headers: %s, conn:'%s'" % (self.close_connection, str(self.headers), self.headers.get('Connection', "").lower()))
conntype = self.headers.get('Connection', "").lower()
if conntype != 'keep-alive':
self.close_connection = True
self.log_message("ka:'%s', pv:%s[%s]" % (str(conntype == 'keep-alive'), str(self.protocol_version >= "HTTP/1.1"), self.protocol_version))
def do_POST(self):
"""Respond to a POST request."""
response = b"hello post"
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
if self.path == "/empty":
self.finish()
return
if self.path == "/timeout":
time.sleep(2)
if self.path == "/error_403":
self.send_response(403)
else:
self.send_response(200)
if self.path == "/map-simple":
response = b"hello map"
self.send_header("Content-Length", str(len(response)))
conntype = self.headers.get('Connection', "").lower()
if conntype != 'keep-alive':
self.close_connection = True
else:
self.send_header("Connection", "keep-alive")
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(response)
self.log_message("to be closed: %d, headers: %s, conn:'%s'" % (self.close_connection, str(self.headers), self.headers.get('Connection', "").lower()))
self.log_message("ka:'%s', pv:%s[%s]" % (str(conntype == 'keep-alive'), str(self.protocol_version >= "HTTP/1.1"), self.protocol_version))
class ThreadingSimpleServer(socketserver.ThreadingMixIn,
http.server.HTTPServer):
def __init__(self):
self.allow_reuse_address = True
self.timeout = 1
http.server.HTTPServer.__init__(self, (HOST_NAME, PORT), MyHandler)
def run(self):
dummy_killer.write_pid(PID)
try:
while 1:
sys.stdout.flush()
server.handle_request()
except KeyboardInterrupt:
print("Interrupt")
except socket.error:
print("Socket closed")
def stop(self):
self.keep_running = False
self.server_close()
if __name__ == '__main__':
server = ThreadingSimpleServer()
dummy_killer.setup_killer(server, server.stop)
server.run()
|