how to get any available port

Fredrik Lundh fredrik at pythonware.com
Tue Oct 4 18:02:23 EDT 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()
("0.0.0.0", 4711)

</F> 






More information about the Python-list mailing list