how to get any available port

Grant Edwards grante at visi.com
Tue Oct 4 17:42:12 EDT 2005


On 2005-10-04, Paul Rubin <> wrote:
> Grant Edwards <grante at visi.com> writes:
>> > In the nomenclature of some of these applications, that kind
>> > of transfer is called a client to client connection.  Both
>> > ends are called clients.
>> 
>> IIRC, we were talking about TCP sockets.

> So, there's a reasonable application for wanting to open a
> listener port without binding any specific port number.  Alice
> would just ask her OS to assign her a port (say it assigns
> 23789) and listen on it, instead of having to contend with
> other apps on the same IP for some specific port number.  Then
> she'd send a message through the chat server asking Bob to
> connect to port 23789 on her machine.

Ah.  Got it.

> I'm not sure if there's a way to do this.  Do you happen to
> know?

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.

-- 
Grant Edwards                   grante             Yow!  Life is a POPULARITY
                                  at               CONTEST! I'm REFRESHINGLY
                               visi.com            CANDID!!



More information about the Python-list mailing list