How to Get Open Port Banner in Python
Last Updated :
03 Jun, 2022
In this article, we will see how to Get Open Port Banner in Python. Here we will discuss
What is Port?
Port is a terminology used on computer networks. You might know, that whenever you connect to the internet, your system or any device to which you are connecting is assigned an IP address. So, when you search for something on the internet, your IP address is sent to the server where your page or response resides. After getting a request, the server sends back the desired page to your system. Now, suppose you have opened various tabs and gone to different websites, like YouTube or chrome or Amazon or any website. Then here, how the computer knows which request is for which tab, this is done with the help of a port number. Ports are nothing but logical entities used for our system to make connections with the network. There are port ranges and each port functions for a particular work. The port ranges from 1 to 65535. The ports range from 49152 to 65535 and are used by client browsers. So, when you request something on the network, a port number between 49152 and 65535 is assigned and that is unique. This port number can be reassigned again, once the session is closed.
What is Banner?
Banner is the description that the server returns. It contains a description of the host system like software type, system version, etc. This banner must be kept hidden as attackers or hackers can use this banner to attack by exploiting any loop while using the system description. And banner grabbing is nothing but getting the banner on a system on the network and banner grabbing is a crime if practiced without required permission.
What is Open Port?
Ports are open and closed. If a port is not open, we won’t be able to make a connection between two systems. So, when we do any activity on the network, the required ports are open and, as a result, we get the response in our system. Now, after discussing these three keywords, you will be able to understand what we are actually trying to do. To get an open port banner we will use a socket module. The socket is a way of connecting two nodes on a network to communicate with each other. One node listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.
Example 1
First, let’s see how we can see which ports are open for our system for that we are using threading for fast computation. We can get the IP address of the localhost by passing the host variable to the gethostbyname() function of the socket module. Here AF_INET specifies the IP address is IPV4(Internet Protocol version 4) and SOCKET_STREAM specifies it’s a TCP socket. Next, we have set the status=False, which will be true whenever we make a connection. connect() function is used to make connections between host_ip and port.
Python3
import socket
import threading
import time
def scan_port(port):
host = "localhost"
host_ip = socket.gethostbyname(host)
status = False
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try :
s.connect((host_ip, port))
status = True
except :
status = False
if status:
print ( "port {} is open" . format (port))
start_time = time.time()
for i in range ( 0 , 100000 ):
thread = threading.Thread(target = scan_port, args = [i])
thread.start()
end_time = time.time()
print ( "To all scan all ports it took {} seconds" . format (end_time - start_time))
|
Example 2
Here you can see we have added s.recv(1024).decode(). This means the socket will return the banner in 1024 bytes of buffer size and then we decode it to a string. Now to get banners on these open ports, we need to add just one more line after making the connection, banner = s.recv(1024).decode().
Python3
import socket
import threading
import time
def scan_port(port):
try :
host = "localhost"
host_ip = socket.gethostbyname(host)
status = False
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host_ip, port))
try :
banner = s.recv( 1024 ).decode()
print ( "port {} is open with banner {}" . format (port, banner))
except :
print ( "port {} is open " . format (port))
except :
pass
start_time = time.time()
for i in range ( 0 , 100000 ):
thread = threading.Thread(target = scan_port, args = [i])
thread.start()
end_time = time.time()
print ( "To scan all ports it took {} seconds" . format (end_time - start_time))
|
Output:

We can see that port 22 is open with its banner information.
Similar Reads
How to Capture udp Packets in Python
Capturing UDP (User Datagram Protocol) packets is a fundamental skill for network programmers and security enthusiasts. Python, being a versatile and powerful programming language, offers various libraries and tools to facilitate packet capture. In this article, we'll explore how to capture UDP pack
3 min read
How to check if an application is open in Python?
This article is about How to check if an application is open in a system using Python. You can also refer to the article Python â Get list of running processes for further information. In the below approaches, we will be checking if chrome.exe is open in our system or not. Using psutil The psutil is
2 min read
Get OS name and version in Python
Python programming has several modules that can be used to retrieve information about the current operating system and the version that is running on the system. In this article we will explore How to get the OS name and version in Python. Let us see a simple example to get the OS name and version i
2 min read
How to Make a Process Monitor in Python?
A process monitor is a tool that displays the system information like processes, memory, network, and other stuff. There are plenty of tools available, but we can make our own process monitor using Python. In Python, there is a module called psutil that we can use to grab various information about o
3 min read
How to Check Open Ports Using CMD in Windows?
Open ports on your Windows computer act like doors for data-letting information in and out. While necessary for apps and services, unprotected ports can become security risks. Checking which ports are open helps you spot vulnerabilities, fix connection issues, and keep your system safe. Using Comman
5 min read
How to open an image from the URL in PIL?
In this article, we will learn How to open an image from the URL using the PIL module in python. For the opening of the image from a URL in Python, we need two Packages urllib and Pillow(PIL). Approach:Install the required libraries and then import them. To install use the following commands:pip ins
1 min read
Pandas.get_option() function in Python
Pandas have an options system that lets you customize some aspects of its behavior, display-related options being those the user is most likely to adjust. Let us see how to see the value of a specified option. get_option() Syntax : pandas.get_option(pat)Parameters : pat : Regexp which should match a
2 min read
How to Get Data from API in Python Flask
In modern web development, APIs (Application Programming Interfaces) play a crucial role in enabling the interaction between different software systems. Flask, a lightweight WSGI web application framework in Python, provides a simple and flexible way to create APIs. In this article, we'll explore ho
2 min read
Python | os.get_exec_path() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in the case of invalid or inaccessible f
2 min read
How to Install and use SSL Certificate In Python
A secure Socket Layer (SSL) Certificate is a Digital certificate that can be used for the authentication of a website and it helps to establish an encrypted connection between the user and server. SSL is a secure layer that creates an encrypted link between a web server and a web browser. SSL keeps
2 min read