Commit f55b84ac authored by Matt Traudt's avatar Matt Traudt
Browse files

Add simple HTTP server script

parent 57006a43
#!/usr/bin/env python3
# File:
# Author: Matt Traudt
# License: CC0
# This script implements just enough of the HTTP protocol to work with Simple
# Bandwidth Scanner.
# All requested URLs exist. All return 1 GiB of garbage data. We always speak
# HTTP/1.1 because that's necessary for Keep-Alive request headers
# (used by sbws scanners) to work.
# HEAD and GET requests are supported to the minimum extent necessary.
# This essentially means that if the client sends Range request headers just
# like sbws does, then we'll only send back the number of bytes they requested.
# Indeed, this was the motivating reason for the complexity of this script;
# normally I would have used SimpleHTTPRequestHandler unmodified.
# Don't breathe too hard or this script might break.
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import http.server
from http import HTTPStatus
# import time
FILE_SIZE = 1*1024*1024*1024 # 1 GiB
def _get_resp_size_from_range(range_str):
assert range_str.startswith('bytes=')
range_str = range_str[len('bytes='):]
start_byte, end_byte = range_str.split('-')
return int(end_byte) - int(start_byte) + 1
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
def send_head(self, length):
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Length', length)
# self.send_header('Last-Modified', self.date_time_string(time.time()))
def do_GET(self):
range_hdr = self.headers['Range']
if not range_hdr:
num_bytes = FILE_SIZE
assert range_hdr.startswith('bytes=')
num_bytes = _get_resp_size_from_range(range_hdr)
self.wfile.write(b'A' * num_bytes)
def do_HEAD(self):
def main(args):
addr = ('', args.port)
print('Listening on', addr)
httpd = http.server.HTTPServer(addr, MyHTTPRequestHandler)
if __name__ == '__main__':
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
'-p', '--port', default=8000, type=int, help='Port on which to listen')
args = parser.parse_args()
except KeyboardInterrupt:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment