Skip to content

Instantly share code, notes, and snippets.

@lasteminista
Created May 9, 2022 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lasteminista/bd0b78ea9088f3411ae2885f070ec81d to your computer and use it in GitHub Desktop.
Save lasteminista/bd0b78ea9088f3411ae2885f070ec81d to your computer and use it in GitHub Desktop.
Port Scanner Code Snippet 3
import socket
from datetime import datetime
import threading
def get_target():
hostname = input("Enter your target hostname (or IP address) : ")
target = socket.gethostbyname(hostname)
print(f'Scan Target > {target}')
return target
def get_port_list():
print(f'Ports Range > [1 - 1024]')
return range(1, 1024)
def scan_port(target, port):
# Create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# Test connection
test = s.connect_ex((target, port))
if test == 0:
print(f'Port {port} is [open]')
def port_scanner():
try:
target = get_target()
port_list = get_port_list()
thread_list = list()
start_time = datetime.now()
for port in port_list:
scan = threading.Thread(target=scan_port, args=(target, port))
thread_list.append(scan)
scan.daemon = True
scan.start()
for scan in thread_list:
scan.join()
except:
print("Something went wrong !")
else:
end_time = datetime.now()
print("Scanning completed in", end_time - start_time)
if __name__ == '__main__':
port_scanner()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment