how to get any available port

Paul Rubin http
Tue Oct 4 18:22:21 EDT 2005


"Fredrik Lundh" <fredrik at pythonware.com> writes:
> 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)

Nice.  Thanks.



More information about the Python-list mailing list