Skip to content
Related Articles

Related Articles

Simple Port Scanner using Sockets in Python
  • Last Updated : 01 Aug, 2020

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 the ports for any services.

Example 1: In this program, you can scan a number of ports in a certain range.

Python3




# Here we import two modules, socket and time
  
import socket
import time
  
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
# here we asking for the target website
# or host
target = input('What you want to scan?: ')
  
# next line gives us the ip address
# of the target
target_ip = socket.gethostbyname(target)
print('Starting scan on host:', target_ip)
  
# function for scanning ports
  
  
def port_scan(port):
    try:
        s.connect((target_ip, port))
        return True
    except:
        return False
  
  
start = time.time()
  
# here we are scanning port 0 to 4
for port in range(5):
    if port_scan(port):
        print(f'port {port} is open')
    else:
        print(f'port {port} is closed')
  
end = time.time()
print(f'Time taken {end-start:.2f} seconds')

Output:



What you want to scan?: localhost
Starting scan on host: 127.0.0.1
port 0 is closed
port 1 is closed
port 2 is closed
port 3 is closed
port 4 is closed
Time taken 8.12 seconds

Note: you can change the range in the for loop for change the number of ports to be scanned. For scanning a website or a host it can take a certain time so be patient.

Example 2: If you want to scan a particular port then go for this solution.

Python3




# importing the scokets module
import socket
  
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
target = input('What you want to scan?: ')
  
# getting the ip address using gethostbyname
# function
t_IP = socket.gethostbyname(target)
print("Starting scan on host: ", t_IP)
  
  
def port_scan(port):
    try:
        s.connect((t_IP, port))
        return True
    except:
        return False
  
  
port = int(input("Enter the port number to be scanned: "))
  
if port_scan(port):
    print('Port', port, 'is open')
else:
    print("port", port, "is closed")

Output:

What you want to scan?: localhost
Starting scan on host:  127.0.0.1
Enter the port number to be scanned: 135
Port 135 is open

Note: Here we are scanning the localhost. You can scan any host or website. If you are getting any error then sockets is unable to connect the target or perhaps you did some mistake in your code.

Warnings: Without taking permission from the administration scanning ports of a server or a website can be considered as a crime. There are many free websites available for testing, you can use them.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :