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

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?



Solution 1:[1]

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

Solution 2:[2]

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()

Solution 3:[3]

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

Solution 4:[4]

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.

Solution 5:[5]

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

Solution 6:[6]


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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rkachach
Solution 2 yig
Solution 3 Mox
Solution 4 eemz
Solution 5 Kiran Mali
Solution 6 ckloan