WebMasterCampus
WEB DEVELOPER Resources

Python Port Scanner Using Socket Bind Function

Learn how to build Python Port Scanner Socket Bind Function


Python Port Scanner Using Socket Bind Function

import socket
import sys
from datetime import datetime


start_time = datetime.now()

print("Start Date & Time=", start_time)
with open("port-scanner-3.txt",'w') as file:
    print("Start Date & Time=", start_time, file=file)

try:
    input_host_name = input("Please Enter host to Scan: ")
    host_ip = socket.gethostbyname(input_host_name)
except socket.gaierror:
    print("HostName Could not be resolved")
    print("\n\nHostName Could not be resolved", file=file)
    sys.exit()


for port in range(1, 1025):
    try:
        # AF_INET means IP4 address. AF_INET6 means IP6 address
        # SOCK_STREAM means we are using TCP. SOCK_DGRAM for UDP
        serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serv.bind((host_ip, port))

    except :
        with open("port-scanner-3.txt", 'a') as file:
            print("Open Port ", port)
            print("\nOpen Port ", port, file=file)


end_time = datetime.now()

print("End  Date & Time=", end_time)
with open("port-scanner-3.txt",'a') as file:
    print("\n\nEnd Date & Time=", end_time, file=file)


print("Time Taken", end_time - start_time)
with open("port-scanner-3.txt",'a') as file:
    print("\n\nTime Taken", end_time - start_time, file=file)
Created with love and passion.