-
Notifications
You must be signed in to change notification settings - Fork 43
/
testserver.py
executable file
·79 lines (63 loc) · 1.52 KB
/
testserver.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
#! /usr/bin/env python
import time
import BaseHTTPServer
import urlparse
HOST_NAME = 'localhost'
PORT_NUMBER = 49000
def makeBestType(s):
try:
return int(s)
except:
pass
try:
return float(s)
except:
pass
return s
def sendFile(s, numBytes=100, delay=0.0, blockSize=100):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.send_header("Content-Length", "%d" % numBytes)
s.end_headers()
sentBytes = 0
while sentBytes < numBytes:
time.sleep(delay)
actualBytes = (sentBytes + blockSize)
if actualBytes > numBytes:
actualBytes = numBytes
s.wfile.write("a" * (actualBytes - sentBytes))
s.wfile.flush()
sentBytes = actualBytes
print sentBytes
responses = {
"/file" : sendFile
}
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
"""Respond to a GET request."""
if self.headers.has_key('If-Modified-Since'):
self.send_response(304)
return
components = urlparse.urlsplit(self.path)
path = components[2]
params = components[3].split('&')
print params
parameters = {}
for p in params:
k, v = p.split('=')
v = makeBestType(v)
parameters[k] = v
if responses.has_key(path):
responses[path](self, **parameters)
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()