check for unused ports and then grab one
Brad Tilley
bradtilley at usa.net
Mon Sep 13 19:20:40 EDT 2004
Peter Hansen wrote:
> Brad Tilley wrote:
>
>> Instead of me arbitrarily assigning a high port number to a variable,
>> is it possible to check for ports that are unused and then randomly
>> assign one of them to a variable? Something like this is what I'm
>> thinking:
>>
>> port = socket.getunusedport()
>>
>> Any ideas?
>
>
> As Erik says, use a loop until you get a free one, catching the
> exceptions that are raised as you try to use the occupied ones.
>
> I'm curious what you are trying to do though. Normally a server
> has to listen on a predefined port or the clients won't be able
> to connect, and a client doesn't need to specify which port it
> will bind to as the OS will pick a free one automatically. I've
> never had to do what you are trying to do, thus my curiosity...
>
> -Peter
I use Python for administering windows clients. This software will
facilitate that... that's all.
The server (which resides on the win clients) has to start and configure
itself dynamically. Using the same port number isn't necessary and I
prefer not to hard code it. Each server will have a client component
that grabs the current IP (dhcp) and port number the server is using and
then sends it to a separate machine (sys-admin workstation) that is
collecting a list of ips and ports for later usage.
Brad
More information about the Python-list
mailing list