add helper for serving a directory (#31802)

directory http server
old-commit-hash: 358461896c115027a6372504505a4cd9be8dbeb2
This commit is contained in:
Justin Newberry
2024-03-08 16:09:47 -05:00
committed by GitHub
parent 6bf5c62155
commit 52a32bf132
+9
View File
@@ -113,3 +113,12 @@ def with_http_server(func, handler=http.server.BaseHTTPRequestHandler, setup=Non
with http_server_context(handler, setup) as (host, port):
return func(*args, f"http://{host}:{port}", **kwargs)
return inner
def DirectoryHttpServer(directory) -> type[http.server.SimpleHTTPRequestHandler]:
# creates an http server that serves files from directory
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=str(directory), **kwargs)
return Handler