python,获取指定端口的状态

2025-03-29 16:19:42
推荐回答(1个)
回答1:

import socket
def isPortFree(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.bind(('127.0.0.1', port))
    except Exception,e:
        sock.close()
        print e
        return False

    sock.close()
    return True