used ports - socket bind
Peter Hansen
peter at engcorp.com
Mon Apr 15 21:37:14 EDT 2002
Jesper Olsen wrote:
>
> When binding a socket to a port, is there a way to test if the port
> is already used?
>
> (Apart from catching the resulting exception if it is...)
Even if there was a way, it wouldn't be correct to use it.
This is no different from testing whether a file exists
before trying to open it, or many other examples.
Between the time you discover that the port is not in use
and the time you attempt to bind to it, something else might
already have bound to that port, so you'd still get the
exception and still have to catch and handle it.
So save yourself the trouble and just do the try/except
approach. (And I'm pretty sure the answer is "no".)
-Peter
More information about the Python-list
mailing list