Fast way to test if a port is in use using Python

Question:

I have a python server that listens on a couple sockets. At startup, I try to connect to these sockets before listening, so I can be sure that nothing else is already using that port. This adds about three seconds to my server’s startup (which is about .54 seconds without the test) and I’d like to trim it down. Since I’m only testing localhost, I think a timeout of about 50 milliseconds is more than ample for that. Unfortunately, the socket.setdefaulttimeout(50) method doesn’t seem to work for some reason.

How I can trim this down?

Asked By: directedition

||

Answers:

How about just trying to bind to the port you want, and handle the error case if the port is occupied?
(If the issue is that you might start the same service twice then don’t look at open ports.)

This is the reasonable way also to avoid causing a race-condition, as @eemz said in another answer.

Answered By: Simon B.

Are you on Linux? If so, perhaps your application could run netstat -lant (or netstat -lanu if you’re using UDP) and see what ports are in use. This should be faster…

Answered By: Mox

Simon B’s answer is the way to go – don’t check anything, just try to bind and handle the error case if it’s already in use.

Otherwise you’re in a race condition where some other app can grab the port in between your check that it’s free and your subsequent attempt to bind to it. That means you still have to handle the possibility that your call to bind will fail, so checking in advance achieved nothing.

Answered By: eemz

Here is an example of how to check if the port is taken.

import socket, errno

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind(("127.0.0.1", 5555))
except socket.error as e:
    if e.errno == errno.EADDRINUSE:
        print("Port is already in use")
    else:
        # something else raised the socket.error exception
        print(e)

s.close()
Answered By: Stian OK

To check port use:

def is_port_in_use(port: int) -> bool:
    import socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        return s.connect_ex(('localhost', port)) == 0

source: https://codereview.stackexchange.com/questions/116450/find-available-ports-on-localhost

Note:
connect_ex cann still raise an exception (in case of bad host name i.e see docs on [1].

[1] https://docs.python.org/3/library/socket.html

Answered By: Rugnar

Python 2 code to check port is in use or not

# python 2
import socket

def check_port(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    res = sock.connect_ex(('localhost', port))
    sock.close()
    return res == 0

Check here for python 3

Answered By: Kiran Mali

def check_port(i):
    import socket, errno
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:        s.bind(("127.0.0.1", i))
    except socket.error as e:
        if e.errno == errno.EADDRINUSE:
            print("Port is already in use",i)
        else:
            print(e)
            return False   
    s.close()
    return True


#Check port open for range of ports

for i in range (10000):
    check_port(i)
Answered By: ckloan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.