Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > check for unused ports and then grab one

Reply
Thread Tools

check for unused ports and then grab one

 
 
Brad Tilley
Guest
Posts: n/a
 
      09-13-2004
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?
 
Reply With Quote
 
 
 
 
Erik Heneryd
Guest
Posts: n/a
 
      09-13-2004
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?


No. Trial and error until you find one.


Erik
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      09-13-2004
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
 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      09-13-2004
In article <(E-Mail Removed)>,
Erik Heneryd <(E-Mail Removed)> 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?

>
>No. Trial and error until you find one.

.
.
.
Incorrect, if I understand you both; *UNIX Network Programming*
has said for years that
The process can let the system automatically assign
a port. For both the Internet domain and the XNS
domain, specifying a port number of 0 before calling
bind() requests the system to do this.
While I've never tracked down an RFC that specifies this, it surely
exists.
 
Reply With Quote
 
Josh Close
Guest
Posts: n/a
 
      09-13-2004
I believe you should be able to just bind to port 0. Then it will pick
up an available one and you won't have to worry about it.

-Josh


On Mon, 13 Sep 2004 22:08:10 GMT, Cameron Laird <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> Erik Heneryd <(E-Mail Removed)> 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?

> >
> >No. Trial and error until you find one.

> .
> .
> .
> Incorrect, if I understand you both; *UNIX Network Programming*
> has said for years that
> The process can let the system automatically assign
> a port. For both the Internet domain and the XNS
> domain, specifying a port number of 0 before calling
> bind() requests the system to do this.
> While I've never tracked down an RFC that specifies this, it surely
> exists.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
Michael Fuhr
Guest
Posts: n/a
 
      09-13-2004
Erik Heneryd <(E-Mail Removed)> writes:

> 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?

>
> No. Trial and error until you find one.


On many systems, binding to port 0 will give you an available port;
you can then use getsockname() to find out which port you got.

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.bind(('', 0))
sock.listen(socket.SOMAXCONN)
ipaddr, port = sock.getsockname()
print 'listening on %s port %d' % (ipaddr, port)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
 
Reply With Quote
 
Brad Tilley
Guest
Posts: n/a
 
      09-13-2004
Cameron Laird wrote:
> In article <(E-Mail Removed)>,
> Erik Heneryd <(E-Mail Removed)> 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?

>>
>>No. Trial and error until you find one.

>
> .
> .
> .
> Incorrect, if I understand you both; *UNIX Network Programming*
> has said for years that
> The process can let the system automatically assign
> a port. For both the Internet domain and the XNS
> domain, specifying a port number of 0 before calling
> bind() requests the system to do this.
> While I've never tracked down an RFC that specifies this, it surely
> exists.


This works... even on winXP... thank you!

import socket

def get_server():
server = socket.gethostbyname(socket.gethostname())
return server

def get_port():
port = 0
return port

def listen(server_param, port_param):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((server_param, port_param))
s.listen(1)
ipaddr, port = s.getsockname()
print ipaddr, port


-------------------------------------------------
IDLE 1.0.3 ==== No Subprocess ====
>>>

192.168.1.100 1079
>>>

192.168.1.100 1080
>>>

192.168.1.100 1081
>>>

192.168.1.100 1082
>>>

192.168.1.100 1083
>>>

192.168.1.100 1084
>>>



 
Reply With Quote
 
Brad Tilley
Guest
Posts: n/a
 
      09-13-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
 
Reply With Quote
 
Erik Heneryd
Guest
Posts: n/a
 
      09-14-2004
Brad Tilley wrote:
> Cameron Laird wrote:
>> Incorrect, if I understand you both; *UNIX Network Programming*
>> has said for years that
>> The process can let the system automatically assign a port. For
>> both the Internet domain and the XNS
>> domain, specifying a port number of 0 before calling
>> bind() requests the system to do this.
>> While I've never tracked down an RFC that specifies this, it surely
>> exists.


Ah. Nothing you use very often. Closest I've come is opening a
(random) port in some interval.

> This works... even on winXP... thank you!


Not very surprising considering that it's network code is based on BSD.


Erik
 
Reply With Quote
 
Brad Tilley
Guest
Posts: n/a
 
      09-14-2004
Erik Heneryd wrote:

> Brad Tilley wrote:
>
>> Cameron Laird wrote:
>>
>>> Incorrect, if I understand you both; *UNIX Network Programming*
>>> has said for years that
>>> The process can let the system automatically assign a port. For
>>> both the Internet domain and the XNS
>>> domain, specifying a port number of 0 before calling
>>> bind() requests the system to do this.
>>> While I've never tracked down an RFC that specifies this, it surely
>>> exists.

>
>
> Ah. Nothing you use very often. Closest I've come is opening a
> (random) port in some interval.
>
>> This works... even on winXP... thank you!

>
>
> Not very surprising considering that it's network code is based on BSD.
>
>
> Erik


Yeah, if it wasn't for the BSD license, Microsoft and Apple would have
been forced to write more code than they have... there really is a
merciful God
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard or Easy? To find string, then grab criterion matched lines above and below? samiam@mytrashmail.com Perl Misc 13 10-09-2006 07:56 PM
How does one play a .wav file and then another and then another...? ted.j.conway@gmail.com Javascript 4 01-17-2006 03:28 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Re: unused input ports Mario Trams VHDL 0 07-22-2003 08:18 AM



Advertisments