从首选端口开始向后查找可用端口。 直接按实际监听地址探测,避免 0.0.0.0 与 127.0.0.1 探测结果不一致。
(host: str, preferred_port: int, max_scan: int = 100)
| 57 | |
| 58 | |
| 59 | def _find_available_port(host: str, preferred_port: int, max_scan: int = 100) -> int: |
| 60 | """ |
| 61 | 从首选端口开始向后查找可用端口。 |
| 62 | 直接按实际监听地址探测,避免 0.0.0.0 与 127.0.0.1 探测结果不一致。 |
| 63 | """ |
| 64 | probe_host = host or "0.0.0.0" |
| 65 | for offset in range(max_scan): |
| 66 | candidate = preferred_port + offset |
| 67 | if _can_bind_port(probe_host, candidate): |
| 68 | return candidate |
| 69 | return preferred_port |
| 70 | |
| 71 | |
| 72 | def _get_display_host(host: str) -> str: |
no test coverage detected