forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedtestserver
executable file
·93 lines (77 loc) · 2.86 KB
/
speedtestserver
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
#!/usr/bin/env python
# A dummy http server used for testing http download clients.
# Base URL filename doesn't matter, bytes served will be semi-random.
#
# Queries this server handles (append to base url):
# ?bytes=N (approximate # bytes to be served)
# ?timeout=N (seconds delay in the middle of the page)
# ?delay=N (milliseconds delay between bytes)
import time
import http.server
import cgi
import os
HOST_NAME = os.uname()[1]
PORT_NUMBER = 9000
class SpeedTestHandler(http.server.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write("""<html>
<head>
<title>Web testing server</title>
</head>""")
# If someone went to "http://something.somewhere.net/foo/bar/",
# then s.path equals "/foo/bar/".
s.wfile.write("<p>You accessed path: %s at %s</p>" % (s.path,
time.asctime()))
nbytes = 1000
timeout = 0
delay = 0
# Parse any CGI-style parameters
if '?' in s.path:
qs = s.path.split('?')[1]
queries = cgi.parse_qs(qs)
if 'timeout' in queries:
timeout = int(queries['timeout'][0])
s.wfile.write("<br>Timeout: %s" % timeout)
if 'bytes' in queries:
nbytes = int(queries['bytes'][0])
s.wfile.write("<br>Bytes: %s" % nbytes)
if 'delay' in queries:
delay = int(queries['delay'][0])
s.wfile.write("<br>Delay: %s" % delay)
data = 'a' * (nbytes/2)
s.wfile.write("<p>\n")
SpeedTestHandler.send_at_speed(s, data, delay)
s.wfile.write("\n<p>%s</p><p>\n" % time.asctime())
if timeout > 0:
time.sleep(timeout)
SpeedTestHandler.send_at_speed(s, data, delay)
s.wfile.write("</p>")
s.wfile.write("<p>End download at %s</p>" %time.asctime())
s.wfile.write("</body></html>")
def send_at_speed(s, data, delay):
if not delay:
s.wfile.write(data)
return
for b in data:
s.wfile.write(b)
time.sleep(delay)
if __name__ == '__main__':
server_class = http.server.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), SpeedTestHandler)
print("%s: Server Starts - %s:%s" % (time.asctime(),
HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("%s: Server Stops - %s:%s" % (time.asctime(),
HOST_NAME, PORT_NUMBER))