Port Scanner using Python Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisites: Socket Programming in Python This article is just to provide a sample code to generate a Port Scanner. This Port Scanner will work for both the Web Applications as well as remote Host. This tool has been created to provide the basic functionality of a Port Scanner. The general concept of Sockets had been used to provide the functionality. Port Scanner is built on Python 3 and uses some extra libraries such as socket and pyfiglet (for a fancy banner). Please find the below source code for the Port Scanner : Source Code Python3 import pyfiglet import sys import socket from datetime import datetime ascii_banner = pyfiglet.figlet_format("PORT SCANNER") print(ascii_banner) # Defining a target if len(sys.argv) == 2: # translate hostname to IPv4 target = socket.gethostbyname(sys.argv[1]) else: print("Invalid amount of Argument") # Add Banner print("-" * 50) print("Scanning Target: " + target) print("Scanning started at:" + str(datetime.now())) print("-" * 50) try: # will scan ports between 1 to 65,535 for port in range(1,65535): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket.setdefaulttimeout(1) # returns an error indicator result = s.connect_ex((target,port)) if result ==0: print("Port {} is open".format(port)) s.close() except KeyboardInterrupt: print("\n Exiting Program !!!!") sys.exit() except socket.gaierror: print("\n Hostname Could Not Be Resolved !!!!") sys.exit() except socket.error: print("\ Server not responding !!!!") sys.exit() Output: Note: In the above code at line 27 i.e for the port in range(1, 65535): you can custom define your ports under which range you have to scan. This port scanner will generally take the time of 2 mins maximum to produce output in the format, that so and so ports are open or closed. Comment More infoAdvertise with us Next Article Threaded Port Scanner using Sockets in Python D deepamanknp Follow Improve Article Tags : Python Web Technologies Python-socket Practice Tags : python Similar Reads Simple Port Scanner using Sockets in Python Prerequisites: Socket Programming in Python Before going to the programming, let us discuss about ports. In this article, we will check the virtual ports of a server or websites, or localhost. Every port has a unique number. There are 65,535 ports available in a host starting from 0. We can assign t 3 min read Threaded Port Scanner using Sockets in Python Port scanning can be really slow yet, in most cases, is not process intensive. Thus, we can use threading to improve our speed. There can be thousands of possible ports. If it takes 5-15 seconds per port to scan, then we might have a long wait ahead of us without the use of threading. Threading Thre 2 min read Python - Simple Port Scanner with Sockets Ports can be scanned to check which ports are engaged and which ports are open or free. In Python "Socket" module provides access to the BSD socket interface, which is available on all platforms. To scan the ports, the following steps can be implemented: 1] Recognize the host's IP address 2] Create 2 min read File Sharing App using Python Computer Networks is an important topic and to understand the concepts, practical application of the concepts is needed. In this particular article, we will see how to make a simple file-sharing app using Python. Â An HTTP Web Server is software that understands URLs (web address) and HTTP (the proto 4 min read Telnet Automation / Scripting Using Python Telnet is the short form of Teletype network, which is a client/server application that works based on the telnet protocol. Telnet service is associated with the well-known port number - 23. As Python supports socket programming, we can implement telnet services as well. In this article, we will lea 5 min read Simple Chat Room using Python This article demonstrates - How to set up a simple Chat Room server and allow multiple clients to connect to it using a client-side script. The code uses the concept of sockets and threading. Socket programming Sockets can be thought of as endpoints in a communication channel that is bi-directional 8 min read Like