Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
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)