Change mock HTTP server to listen on 127.0.0.1 by default
This is so that it works properly on MacOS which does not permit
listening on random loopback addresses for user processes.
@@ -158,8 +158,9 @@ def get_random_ip(parts: List[str] = None) -> str:
@contextmanager
-def ctx_http_server(handler: Type[BaseHTTPRequestHandler]) -> Iterator[HTTPServer]:
- host = get_random_ip()
+def ctx_http_server(
+ handler: Type[BaseHTTPRequestHandler], host: str = "127.0.0.1"
+) -> Iterator[HTTPServer]:
server = HTTPServer((host, 0), handler)
server_thread = Thread(target=server.serve_forever)
server_thread.daemon = True
@@ -391,9 +392,8 @@ class ServedSimpleHTTPMock(SimpleHTTPMock, AbstractContextManager):
... assert req.path == "/bad/path"
"""
- def __init__(self):
+ def __init__(self, host: str = "127.0.0.1"):
super().__init__()
- host = get_random_ip()
self.server = HTTPServer((host, 0), self.Handler)
self.server_thread = Thread(target=self.server.serve_forever)
self.server_thread.daemon = True
