how to get any available port

Fredrik Lundh fredrik at
Wed Oct 5 00:02:23 CEST 2005

Grant Edwards wrote:

> IIRC, you just call bind() with a port number of zero, and then
> use some method-or-other on the bound socket to find out what
> port it's bound to.

>>> s = socket.socket()
>>> s.bind(("", 0))
>>> s.getsockaddr()
("", 4711)


More information about the Python-list mailing list