(port)
return tcp_use or udp_use
判断端口是否占用
判断端口是否占用
判断端口是否占用
def port_is_use(port):
    """
    判断端口是否占用
    """
    tcp_use, udp_use = False, False
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    u = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    s.settimeout(3)
    tcp_use = s.connect_ex(('127.0.0.1', int(port))) == 0
    try:
        u.bind(('127.0.0.1', int(port)))
    except:
        udp_use = True
    finally:
        u.close()
    return tcp_use or udp_use
def port_is_use ( port ) : tcp_use , udp_use = False , False s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) u = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) s . settimeout ( 3 ) tcp_use = s . connect_ex ( ( '127.0.0.1' , int ( port ) ) ) == 0 try : u . bind ( ( '127.0.0.1' , int ( port ) ) ) except : udp_use = True finally : u . close ( ) return tcp_use or udp_use