Zimple : A Simple Port Scanner Made With Python

Muhammad Ghazy
3 min readJan 14, 2023

--

A port scanner is a software tool or program that is used to identify open ports and services on a computer or network. It can scan a range of IP addresses or a specific IP address and check if certain ports are open or closed, and if they are open, it can determine the type of service running on that port. This information can be used for network inventory, security assessments, and vulnerability identification.

import socket

def port_scanner(host, port_range):
for port in port_range:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.5)
result = s.connect_ex((host, port))
if result == 0:
print("Port {} is open".format(port))
s.close()
except:
pass

# This code will scan the first 1024 ports on the host "www.example.com" and print out which ones are open.
# You can adjust the range of ports to scan by changing the arguments to the range function.
port_scanner("www.example.com", range(1, 1024))

As you can see in the first line of code there’s socket module, Basically the socket module in Python provides a low-level network programming interface. It allows you to create and manage network connections using sockets, which are a common programming interface for network communication in many operating systems. The socket module provides a variety of functions and classes for working with sockets, including:

  • socket.AF_INET and socket.AF_INET6 constants for specifying the address family (IPv4 or IPv6)
  • socket.SOCK_STREAM and socket.SOCK_DGRAM constants for specifying the socket type (TCP or UDP)
  • socket.socket() function for creating a new socket
  • socket.bind(), socket.listen(), socket.accept(), socket.connect(), socket.send(), and socket.recv() methods for working with sockets

The socket module can be used for a wide range of network programming tasks, including creating simple client-server applications, implementing network protocols, and more.

In second line there’s defines a function port_scanner(host, port_range) that takes two arguments:

  • host: a string representing the hostname or IP address of the target system to be scanned
  • port_range: a range of integers representing the ports to be scanned

The function then uses a for loop to iterate over the ports in the given range.

For each port, the function creates a new socket using socket.socket() with the parameters socket.AF_INET and socket.SOCK_STREAM, which specifies that the socket is using the IPv4 protocol and TCP protocol.

Then it sets the timeout of the socket to 0.5 seconds using the settimeout() function.

It attempts to connect to the target host on the current port using the connect_ex() function. This function returns 0 if the connection is successful, otherwise it returns an error code.

If the result of the connection is 0, it means the port

is open, so it prints “Port {} is open” where {} is the port number.

Here’s the link to source code in my repo. Also big thanks to Justin Seitz for the inspiration to build this simple easy-to-use port scanner.

--

--

Muhammad Ghazy
Muhammad Ghazy

Written by Muhammad Ghazy

0 Followers

An ordinary CS Student.

No responses yet