Building a basic port scanner using Python.

Benjamin Morales Perez
3 min readMay 23, 2021

Before we dive deep into what network scanning is(and eventually building our own network scanner), we must first understand the basic concepts what networks are and what they do. Networks are the building blocks of every communication device on the planet and almost every single app on your phone. Without networks you wouldn’t be able to text your friends or order food online!

Network scanning is like a thief checking every door and window looking for an entry point to your house, it will check if an entry point is open or closed(door, window etc) but just because an entry point is open, does not mean “it” will go in. It is illegal to use this script without sole permission from the website you intend to scan. Which is why we will be scanning:

A server with cables

What are ports?

Ports are where connections are made to exchange information, a port is a logical construct, a channel for network communications, it’s a way for an application to “know” where to send information.

What are sockets?

Sockets represent a single connection between two applications, Sockets are the combination of a port and a IP address(“Endpoints”)

IP Address?

An IP address is a unique address that identifies a device on the internet, similar to what a physical address does to identify a business or a house.

Before we begin our tutorial, it is important to have Python installed, if you do not have python installed please go to https://www.python.org/ and follow the instructions. Plenty of youtube videos exist that will show you exactly how to install python correctly. Please use those resources if they are available to you.

So our first lines of code are simple:

import socket
import pyfiglet

We are importing two modules:

The socket module helps us create connections, this is the module we will use to scan the ports of a website of our choice.

The pyfiglet module will provide us with a basic title on the terminal, to make it more accessible for other readers/users.

#we set our title to equal to the variable out, using 
#pyfiglet.figlet_format() which allows us to put in any text and #select a font. We then print out.
out = pyfiglet.figlet_format("PortScanner", font="slant")
print(out)
#We take an input and cast it as a stringtarget = str(input("Please enter website: "))#We receive input from the user and use the socket.gethostname() #function to retrieve the IP addressip = socket.gethostbyname(target) #We set a variable called ports with a list of ports as Strings.
ports = ["20", "21", "22", "25", "53", "69", "79", "80", "110", "119", "161", "162", "443"]

Function socket.gethostbyname()?

This function belongs to socket module, which is why we needed to import socket. This function returns the IP address of a host of your choice, it takes a String as a parameter.

#This is our loop, it will loop through the list of ports, and check #if a port is open or not, if it is open it will return Port X Open, 
#if not, it will return Port X Closed.
for i in ports:
try:
sock = socket.socket() #Initializes socket connection
res = sock.connect((ip, i)) #Attempts to connect using port #i
print("Port {} Open".format(i))
sock.close() #Closes connection
except:
print("Port {} Closed" .format(i))

--

--