Checking if port is in use.

A

Alex Polite

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
 
P

Peter Hansen

Alex said:
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
 
A

Alex Polite

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
 
E

elbertlev

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"
 
G

Grant Edwards

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.
 
P

Peter Hansen

Alex said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top