MCPcopy Create free account
hub / github.com/coi/coi / WebServer

Class WebServer

tests/runner/web_base.py:10–30  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8from .base import TestRunnerBase
9
10class WebServer:
11 def __init__(self, root_dir):
12 self.root_dir = root_dir
13 self.proc = None
14 self.port = self._get_free_port()
15
16 def _get_free_port(self):
17 with socket.socket() as s:
18 s.bind(("127.0.0.1", 0))
19 return s.getsockname()[1]
20
21 def __enter__(self):
22 cmd = [sys.executable, "-m", "http.server", str(self.port), "--bind", "127.0.0.1", "--directory", str(self.root_dir)]
23 self.proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
24 time.sleep(0.5)
25 return f"http://127.0.0.1:{self.port}"
26
27 def __exit__(self, exc_type, exc_val, exc_tb):
28 if self.proc:
29 self.proc.terminate()
30 self.proc.wait()
31
32class WebRunnerBase(TestRunnerBase):
33 def __init__(self, root_dir):

Callers 2

runMethod · 0.85
runMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected