Created
November 8, 2012 17:26
-
-
Save jdavis/4040223 to your computer and use it in GitHub Desktop.
Find an open port in Python.
This file contains 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
import socket | |
def find_open_port(): | |
""" | |
Use socket's built in ability to find an open port. | |
""" | |
sock = socket.socket() | |
sock.bind(('', 0)) | |
_, port = sock.getsockname() | |
return port | |
print find_open_port() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment