How to Make Port Scanner in Python?

Posted in /  

How to Make Port Scanner in Python?
vinaykhatri

Vinay Khatri
Last updated on April 24, 2024

    Port Scanner is a tool that comes in very handy when we want to check all the open and close Ports on a system, router, server, or computer. Using the Port scanner we can look out for the opened ports that can be a threat to the system from some malicious attackers.

    In this Python tutorial, you will learn how to write a Python script that can scan a range of ports available in a system, and tell whether they are open or not.

    Python is not an ideal language to design a PORT scanner because scanning all the ports can take a lot of time, which makes the script slower. Although in this tutorial I have also used Python threading so the execution speed of the script can pace up from its actual speed.

    Before we dive into the implementation of Port Scanner in Python let's have a look at the modules we will be using in our Python script.

    Required Modules

    Python socket

    socket is one of the most powerful and widely used Python standard modules. It is a part of the Python suite so you do not need to install it separately. Using the Python Socket module we can perform socket programming in Python and set communication between two nodes present on the network.

    Python threading

    Although Python does not support multi-threading but using the Python standard threading module we can simulate the multi-threading in Python, and make our program execution faster as compared to its original speed. socket and threading both are Python standard modules, which means we do not need to install them using the pip install command.

    Now let's open your best Python IDE or Text Editor and start coding.

    How to Make Port Scanner in Python?

    We will begin with importing all the required modules.

    import socket 
    import threading

    Now let's define the target variable, that represents the IP address of the system and which ports we want to scan.

    For this tutorial, I will be scanning my LocalHost or computer ports, if you want you can scan the port for your server or router.

    target = "127.0.0.1"   # scan local host
    127.0.0.1 represent the IP4 address for the localhost. If you wish to check the Ports for your server or router you need to specify its IP address in the target as a Python string .
    To keep the script Modular, let's define a function that will scan the specified port and check whether it is open or not.
    def port_scanner(port):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((target, port))
            print(f"Port {port} is open")
        except:
            pass

    The port_scanner(port) function will scan the specified port . The socket.socket(socket.AF_INET, socket.SOCK_STREAM) statement will create an instance of the socket s . The socket.AF_INET parameter specifies represent the address is from ipv4 family. And socket.SOCK_STREAM parameter represent that the connection should be TCP oriented. The connect() function will try to connect the target ip address port number.

    If the connection becomes successful this means the port is open and we will print the port number. If the connection becomes unsuccessful we receive an error ConnectionRefusedError that will be handled by the except block. Now let's scan all the ports from range 1 to 5050.

    for port in range(1,5051):
        thread = threading.Thread(target =port_scanner, args=[port])
        thread.start()
    The threading.Thread(target =port_scanner, args=[port]) statement will keep calling the port_scanner function, with port argument. Now put all the code together and execute

    Python program to create a Port Scanner

    import socket 
    import threading
    
    target = "127.0.0.1"   # scan local host
    def port_scanner(port):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((target, port))
            print(f"Port {port} is open")
        except:
            pass
    
    
    for port in range(1,5050):
        thread = threading.Thread(target =port_scanner, args=[port])
        thread.start()
    Output
    Port 21 is open
    Port 80 is open
    Port 135 is open
    Port 443 is open
    Port 445 is open
    Port 3306 is open
    Port 5040 is open

    Conclusion

    The above program will only execute within seconds because here we have to use threading. If you try to execute the same program without using threading, it might take more than 2 or 3 minutes to complete scanning all the ports from 1 to 5050.

    In the above program, we have scanned all the available ports for the local system, you can also scan for your router or server. To get the IP address for your router you can use the ipconfig (windows) or ifconfig (Linux/mac) commands.

    People are also reading:

    Leave a Comment on this Post

    0 Comments