Skip to content

Instantly share code, notes, and snippets.

@butla
Last active December 8, 2022 19:52
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save butla/2d9a4c0f35ea47b7452156c96a4e7b12 to your computer and use it in GitHub Desktop.
Save butla/2d9a4c0f35ea47b7452156c96a4e7b12 to your computer and use it in GitHub Desktop.
Waiting for a TCP port to start accepting connections (Python >=3.6)
"""
MIT License
Copyright (c) 2017 Michał Bultrowicz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import socket
import time
def wait_for_port(port: int, host: str = 'localhost', timeout: float = 5.0):
"""Wait until a port starts accepting TCP connections.
Args:
port: Port number.
host: Host address on which the port should exist.
timeout: In seconds. How long to wait before raising errors.
Raises:
TimeoutError: The port isn't accepting connection after time specified in `timeout`.
"""
start_time = time.perf_counter()
while True:
try:
with socket.create_connection((host, port), timeout=timeout):
break
except OSError as ex:
time.sleep(0.01)
if time.perf_counter() - start_time >= timeout:
raise TimeoutError('Waited too long for the port {} on host {} to start accepting '
'connections.'.format(port, host)) from ex
@eruvanos
Copy link

eruvanos commented Jun 1, 2022

Hi which license is this? Maybe MIT? 😊

@butla
Copy link
Author

butla commented Jun 3, 2022

Hi which license is this? Maybe MIT? blush

Yup, it should be MIT :) Adding...

And I can modernize the code a bit, quickly.

EDIT: done :)

@eruvanos
Copy link

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment