Find an available port in the callback range.
()
| 281 | |
| 282 | |
| 283 | def _find_open_port() -> int: |
| 284 | """Find an available port in the callback range.""" |
| 285 | import socket |
| 286 | |
| 287 | for port in CALLBACK_PORT_RANGE: |
| 288 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 289 | try: |
| 290 | s.bind((CALLBACK_HOST, port)) |
| 291 | return port |
| 292 | except OSError: |
| 293 | continue |
| 294 | raise RuntimeError(f"No available port in range {CALLBACK_PORT_RANGE.start}-{CALLBACK_PORT_RANGE.stop - 1}") |
| 295 | |
| 296 | |
| 297 | # --------------------------------------------------------------------------- |