By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,275 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,275 IT Pros & Developers. It's quick & easy.

Checking if port is in use.

P: n/a

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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.