Wait until a port starts accepting TCP connections. Args: port (int): Port number. host (str): Host address on which the port should exist. timeout (float): In seconds. How long to wait before raising errors. Raises: TimeoutError: The port isn't accepting conn
(port, host="localhost", timeout=5.0)
| 43 | |
| 44 | # From https://gist.github.com/butla/2d9a4c0f35ea47b7452156c96a4e7b12 |
| 45 | def wait_for_port(port, host="localhost", timeout=5.0): |
| 46 | """Wait until a port starts accepting TCP connections. |
| 47 | Args: |
| 48 | port (int): Port number. |
| 49 | host (str): Host address on which the port should exist. |
| 50 | timeout (float): In seconds. How long to wait before raising errors. |
| 51 | Raises: |
| 52 | TimeoutError: The port isn't accepting connection after time specified in `timeout`. |
| 53 | """ |
| 54 | start_time = time.perf_counter() |
| 55 | while True: |
| 56 | try: |
| 57 | with socket.create_connection((host, port), timeout=timeout): |
| 58 | break |
| 59 | except OSError as ex: |
| 60 | time.sleep(0.01) |
| 61 | if time.perf_counter() - start_time >= timeout: |
| 62 | raise TimeoutError( |
| 63 | "Waited too long for the port {} on host {} to start accepting " |
| 64 | "connections.".format(port, host) |
| 65 | ) from ex |
| 66 | |
| 67 | |
| 68 | from selenium import webdriver |
no test coverage detected