Tests: add port argument to python HTTP server

This commit is contained in:
olszomal 2023-05-08 13:20:04 +02:00 committed by Michał Trojnara
parent 54e61cb76a
commit b00ceee310

View File

@ -1,5 +1,6 @@
"""Implementation of a HTTP server""" """Implementation of a HTTP server"""
import argparse
import os import os
import subprocess import subprocess
import sys import sys
@ -91,9 +92,9 @@ class HttpServerThread():
self.server = None self.server = None
self.server_thread = None self.server_thread = None
def start_server(self) -> (int): def start_server(self, port) -> (int):
"""Starting HTTP server on localhost and a random available port for binding""" """Starting HTTP server on localhost and a random available port for binding"""
self.server = ThreadingHTTPServer(('localhost', 0), RequestHandler) self.server = ThreadingHTTPServer(('localhost', port), RequestHandler)
self.server_thread = threading.Thread(target=self.server.serve_forever) self.server_thread = threading.Thread(target=self.server.serve_forever)
self.server_thread.start() self.server_thread.start()
hostname, port = self.server.server_address[:2] hostname, port = self.server.server_address[:2]
@ -104,9 +105,17 @@ class HttpServerThread():
def main() -> None: def main() -> None:
"""Start HTTP server""" """Start HTTP server"""
ret = 0 ret = 0
parser = argparse.ArgumentParser()
parser.add_argument(
"--port",
type=int,
default=0,
help="port number"
)
args = parser.parse_args()
try: try:
server = HttpServerThread() server = HttpServerThread()
port = server.start_server() port = server.start_server(args.port)
with open(PORT_LOG, mode="w") as file: with open(PORT_LOG, mode="w") as file:
file.write("{}".format(port)) file.write("{}".format(port))
except OSError as err: except OSError as err: