Hackerss.com

hackerss
hackerss

Posted on

I want a port scanner in Python

I want a port scanner - Python:


import socket

def port_scanner(ip, port):
    """
    Scan a port on a given IP address.

    Parameters
    ----------
    ip : str
        IP address to scan.
    port : int
        Port to scan.

    Returns
    -------
    bool
        ``True`` if the port is open, ``False`` otherwise.
    """

    #create a socket object
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    #try to connect to the port
    try:
        s.connect((ip, port))
        return True
    except:
        return False
    finally:
        s.close()

#example
result = port_scanner('127.0.0.1', 80)
#output
print(result)  #True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)