1# Copyright 2023 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import functools 6import http 7import logging 8import os 9import threading 10 11from http.server import SimpleHTTPRequestHandler 12 13SRC_DIR = os.path.abspath( 14 os.path.join(os.path.dirname(__file__), *([os.pardir] * 4))) 15 16HTTP_DATA_BASEDIR = os.path.join( 17 SRC_DIR, 'chrome', 'test', 'data', 'chromedriver') 18 19def start_http_server(port:int = 8000, 20 directory: str = None) -> http.server.HTTPServer: 21 """Starts a HTTP server serving the given directory.""" 22 if directory is None: 23 directory = HTTP_DATA_BASEDIR 24 http_server = http.server.HTTPServer(('', port), 25 functools.partial(SimpleHTTPRequestHandler, directory=directory)) 26 logging.info('local http server is running as http://%s:%s', 27 http_server.server_name, http_server.server_port) 28 threading.Thread(target=http_server.serve_forever).start() 29 return http_server 30