Python to Fetch Open Port with Full Source Code

This script finds an open port for the web address.


No need for additional installations.

Run the Script:

  • Save Py file on your local machine and switch to script folder
  • Run following command
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:

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,))
print('Time taken:', time.time() - startTime)


Leave a Comment