-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBaseHTTPServer_threading.py
71 lines (61 loc) · 1.55 KB
/
BaseHTTPServer_threading.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
# coding=utf-8
from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import time,threading
starttime = time.time()
class RequestHandler(BaseHTTPRequestHandler):
def _writeheaders(self,doc):
if doc is None:
self.send_response(404)
else:
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
def _getdoc(self,filename):
global starttime
if filename == '/':
return """
<html>
<head><title>Home Page</title></head>
<body>
<h3>This is Home Page</h3>
<p>Home Page Like This and Nobody Care</p>
</body>
</html>
"""
elif filename == '/stats.html':
return """
<html>
<head><title>Statistics</title></head>
<body>
<h3>This is Statistics Page</h3>
<p>This server has been running for %d seconds</p>
</body>
</html>
"""%int(time.time()-starttime)
else:
return None
def do_HEAD(self):
doc = self._getdoc(self.path)
self._writeheaders(doc)
def do_GET(self):
print "Handling with thread",threading.currentThread().getName()
doc = self._getdoc(self.path)
self._writeheaders(doc)
time.sleep(10)
if doc is None:
self.wfile.write("""
<html>
<head><title>No Found</title></head>
<body>
This requested document '%s' was no found
</body>
</html>
"""%self.path)
else:
self.wfile.write(doc)
class ThreadingHTTPServer(ThreadingMixIn,HTTPServer):
pass
serveraddr = ('',7890)
srvr = ThreadingHTTPServer(serveraddr,RequestHandler)
srvr.serve_forever()