-
Notifications
You must be signed in to change notification settings - Fork 90
/
devserver.py
58 lines (47 loc) · 1.85 KB
/
devserver.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
#!/usr/bin/env python
import SimpleHTTPServer, BaseHTTPServer, SocketServer, socket
class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer) :
"""
New features w/r to BaseHTTPServer.HTTPServer:
- serves multiple requests simultaneously
- catches socket.timeout and socket.error exceptions (raised from
RequestHandler)
"""
def __init__(self, *args):
BaseHTTPServer.HTTPServer.__init__(self,*args)
def process_request_thread(self, request, client_address):
"""
Overrides SocketServer.ThreadingMixIn.process_request_thread
in order to catch socket.timeout
"""
try:
self.finish_request(request, client_address)
self.close_request(request)
except socket.timeout:
print 'Timeout during processing of request from',
print client_address
except socket.error, e:
print e, 'during processing of request from',
print client_address
except:
self.handle_error(request, client_address)
self.close_request(request)
class TimeoutHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""
Abandon request handling when client has not responded for a
certain time. This raises a socket.timeout exception.
"""
# Class-wide value for socket timeout
timeout = 3 * 60
def setup(self):
"Sets a timeout on the socket"
self.request.settimeout(self.timeout)
SimpleHTTPServer.SimpleHTTPRequestHandler.setup(self)
def main():
try:
BaseHTTPServer.test(TimeoutHTTPRequestHandler, ThreadedHTTPServer)
except KeyboardInterrupt:
print '^C received, shutting down server'
if __name__ == '__main__':
main()