Python - Simple Port Scanner with Sockets
Last Updated :
27 Mar, 2023
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 new socket
3] Forming a connection with port
4] Checks whether data is received or not
5] Close connection To use the socket module, we have to import it :
import socket
Let's see some more functions from socket module that can be used to create simple port scanner
To create a new object of socket socket() is used. The syntax of socket() function is:
newSocket = socket.socket(socket_family, socket_type)
socket_family is IP address of version 4 or 6. By default, it takes as IPV4.
AF_INET for socket family of address version 4
AF_INET6 for socket family of address version 6
socket_type is type of connection.by default it takes as TCP connection.
SOCK_STREAM for Socket type of TCP connections
SOCK_DGRAM for Socket type of UDP connections
To Return a string containing the hostname of the machine where the Python interpreter is currently executing. we can use :
socket.gethostname()
If hostname is in IPV6 Following method is used Translate a host name to IPv4 address format. The IPv4 address is returned as a string, such as '10.120.30.2'. If the host name is an IPv4 address itself it is returned unchanged.
socket.gethostbyname(hostname)
To Bind the socket to address, we use bind()method .The socket must not already be bound.The format of address depends on the address family.Syntax of this function is as follows.
socket.bind(self,address)
To close connection close()method is used.This method mark the socket closed. Once that happens, all future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed).Sockets are automatically closed when they are garbage-collected, but it is recommended to close() them explicitly, or to use a with statement around them. Syntax is :
socket.close()
Let us see actual code to scan ports.
Python3
#Python code for simple port scanning
import socket #importing library
ip = socket.gethostbyname (socket.gethostname()) #getting ip-address of host
for port in range(65535): #check for all available ports
try:
serv = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # create a new socket
serv.bind((ip,port)) # bind socket with address
except:
print('[OPEN] Port open :',port) #print open port number
serv.close() #close connection
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
Port Scanner using Python 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
2 min read
Simple chat Application using Websockets with FastAPI In this article, we'll delve into the concept of WebSockets and their role in facilitating bidirectional communication between clients and servers. Subsequently, we'll embark on the journey of building a real-time multi-client chat application. This application will empower numerous users to connect
6 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