Welcome to Python Multi-Threaded Port Scanner with tkinter tutorial series Part 2. In the previous part we have discussed about sockets. The basic idea behind building the port scanner is to scan the ports that are opened on the host server. Open ports relates to the open junction in our previous example.

Warning: Scanning the ports of any host or website or server is a crime. It is crime to perform port scanning against any website or server or host without the permission or written consent from them. This tutorial series is only for educational purpose.

It is good practice to perform scan on https://www.hackthissite.org instead of running it against any other random server or host or website.

Here, the port scanner will attempt to connect to the give port range one by one. If it is able to connect to the port then it will return true else false. This is how hackers scan or test their victim. In this example we will attempt to connect to https://www.hackthissite.org or you can also use your own servers or host.

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server = input('Enter the host to scan?: ')
ip = socket.gethostbyname(server)
def p_scan(port):
    try:
        	con = s.connect((ip,port))
        return True
    except:
        return False


for p in range(20, 25):
    if p_scan(x):
        print('Port {} is opened for {}.format(p, server))

The console will prompt the user for entering the website and our program will attempt to connect to the website at port 20 to 25. If the port will be opened than it will print out the opened port on the console.

Here, we are scanning a few ports but what if we want to scan thousands of port? As the port range from 1 to 65535 this program will take a long time. In next tutorial we will discuss on how we can make this process fast by implementing the concept of multi-threading. Keep sharing. Stay tuned to Tech Tunes.

 

Follow me on Twitter