开发者

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 th开发者_如何学JAVAat. Unfortunately, the socket.setdefaulttimeout(50) method doesn't seem to work for some reason.

How I can trim this down?


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


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


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.


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.


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


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



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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜