Python- monitor whether a port is occupied

! # / usr / bin / env Python
# - * - Coding: UTF-8 - * -

Import socket, Time, the Thread
socket.setdefaulttimeout ( 3)  # Set the default timeout

DEF  socket_port (ip, Port):
    "" "
    input IP and port number, the port determines whether the occupancy scanning
    "" "
    the try:
        iF port> = 65535:
            Print  U 'port scan end'
        S = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
        Result = s.connect_ex ((IP , port))
        IF Result == 0:
            lock.acquire ()
            Print IP, U ':', port, U 'port occupied'
            lock.release ()
    the except:
        Print  U 'port scan abnormality'

def ip_scan(ip):
    "" "
    Enter IP, IP-0-65534 port scan where
    " ""
    the try:
        Print  U 'starts scanning S%'% IP
        START_TIME the time.time = ()
        for I  in Range ( 0, 65534):
            thread.start_new_thread ( socket_port, (IP, int (I)))
        Print  U 'port scan is completed, a total time with:.%. 2F'% (the time.time () - START_TIME)
# the raw_input ( "Press the Enter to the Exit")
    the except:
        Print  U ' scan error ip '

IF __name__ __ == ' __main__ ':
    URL = the raw_input ( ' The ip you want to the Input scan: ')
    Lock thread.allocate_lock = ()
    ip_scan (URL)      

Guess you like

Origin www.cnblogs.com/jcjone/p/12158434.html