Python to Fetch Open Port with Full Source Code

This script finds an open port for the web address.

Prerequisites:

No need for additional installations.

Run the Script:

  • Save Py file on your local machine and switch to script folder
  • Run following command
python3 fetch_open_port.py
Code language: CSS (css)
  • Once script is running, you can write any website you want, and it will print all open ports in range 50-500.

Source Code:

fetch_open_port.py

from socket import *
import time
startTime = time.time()

if __name__ == '__main__':
   target = input('Enter the host to be scanned: ')
   t_IP = gethostbyname(target)
   print ('Starting scan on host: ', t_IP)
   
   for i in range(50, 500):
      s = socket(AF_INET, SOCK_STREAM)
      
      conn = s.connect_ex((t_IP, i))
      if(conn == 0) :
         print ('Port %d: OPEN' % (i,))
      s.close()
print('Time taken:', time.time() - startTime)

Output:

Leave a Comment