Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Forked from jdavis/open_port.py
Created June 25, 2022 21:00
Show Gist options
  • Save ruanbekker/647dbfd9da92bf35d93b275f12e00ffd to your computer and use it in GitHub Desktop.
Save ruanbekker/647dbfd9da92bf35d93b275f12e00ffd to your computer and use it in GitHub Desktop.
Find an open port in Python.
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