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

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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')

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.