Created
December 23, 2013 03:23
-
-
Save allanlei/8091279 to your computer and use it in GitHub Desktop.
Generates a random (assumed available) port
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 random_port(): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.bind(('127.0.0.1', 0)) | |
port = sock.getsockname()[1] | |
sock.close() | |
return port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment