Created
March 6, 2019 14:44
-
-
Save bebosudo/b166e4c618db94fc9020c9e76e1ea7df to your computer and use it in GitHub Desktop.
Search for a free TCP port in python 3 (not completely safe, since between the check and the usage, there could be a race-condition)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import socket | |
def search_free_port(): | |
port = 5000 | |
while True: | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
if s.connect_ex(('localhost', port)) != 0: | |
return port | |
port += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment