469,327 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Checking if port is in use.


If I try to bind a socket to a port that's already in use I get this
error

"error socket.error: (98, 'Address already in use')"

Is there anyway to check in advance if a port i already taken?

alex

--
Alex Polite
http://flosspick.org
Jul 18 '05 #1
5 19288
Alex Polite wrote:
If I try to bind a socket to a port that's already in use I get this
error

"error socket.error: (98, 'Address already in use')"

Is there anyway to check in advance if a port i already taken?


In general in Python it's not considered good style to
"look before you leap". After all, in cases like you
describe, some other could easily grab that port between
the time you discover that it's available and the time
you try to actually open the port.

There are also probably alternative approaches to what
you are trying to do, assuming it's not the case that all
you're doing is trying to avoid the exception. (If that's
the case, just catch the exception: that's how you do it
in Python.)

You could, for example, bind to a port of "0" and that will
auto-assign an available port for you. Does that work
in your case? If not, please describe what you are really
trying to accomplish.

-Peter
Jul 18 '05 #2
On lör, mar 19, 2005 at 10:12:10 -0500, Peter Hansen wrote:
Alex Polite wrote:

You could, for example, bind to a port of "0" and that will
auto-assign an available port for you. Does that work
in your case? If not, please describe what you are really
trying to accomplish.


I'm launching another server (non python) from python and I have to
designate which port that server should bind to.

alex

--
Alex Polite
http://flosspick.org
Jul 18 '05 #3
How about this?

try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
s.bind((HOST, PORT))
except socket.error, e:
if e....
print "address already in use"

Jul 18 '05 #4
On 2005-03-19, Alex Polite <m4@polite.se> wrote:

If I try to bind a socket to a port that's already in use I get this
error

"error socket.error: (98, 'Address already in use')"

Is there anyway to check in advance if a port i already taken?


Yes. Try to bind to the port. If you get teh "Address already
in use" error, then it's already taken.

--
Grant Edwards grante Yow! Is this BOISE??
at
visi.com
Jul 18 '05 #5
Alex Polite wrote:
On lör, mar 19, 2005 at 10:12:10 -0500, Peter Hansen wrote:
Alex Polite wrote:

You could, for example, bind to a port of "0" and that will
auto-assign an available port for you. Does that work
in your case? If not, please describe what you are really
trying to accomplish.


I'm launching another server (non python) from python and I have to
designate which port that server should bind to.


If I understand correctly, then you have only two
clear options:

1. Launch the server on whatever port, and make sure you
can identify its failure when that port is in use.
Presumably then you can just step your way through the
available ports until one of them "takes".

2. Check the port ahead of time, using the pattern of
"bind to port, catch exception if in use, release port if
not in use", then basically do #1, noting that if you
can't catch the server failing you will still have the
slight possibility that something else could grab the
port in the meantime. I suspect that's unlikely to happen
in your case, so you can likely ignore that.

-Peter
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David P. Jessup | last post: by
5 posts views Thread by William Payne | last post: by
99 posts views Thread by Mikhail Teterin | last post: by
2 posts views Thread by GreyAlien007 | last post: by
7 posts views Thread by pradeep_TP | last post: by
2 posts views Thread by simontuohy | last post: by
1 post views Thread by asdf | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.