Port scanner in Python ...
A simple threaded port scanner in python ...
If you don't want to use nmap , zenmap or other port-scanners
then this project is for you...
A disclaimer ... it may not work correctly all the time ..
import socket # pre-installed
import time
import threading
from queue import Queue
socket.setdefaulttimeout(0.25) # setting default time in every port ..
print_lock = threading.Lock()
target = input('Enter the host to be scanned: ') # asking the user for target
t_IP = socket.gethostbyname(target)
print('Starting scan on host: ', t_IP)
def portscan(port): # function for port scanner
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
con = s.connect((t_IP, port))
with print_lock:
print(port, 'is open')
con.close()
except:
pass
def threader(): # function for threading
while True:
worker = q.get()
portscan(worker)
q.task_done()
q = Queue()
startTime = time.time() # starting to record the time the port scanner started ..
for x in range(100): # a for loop
t = threading.Thread(target=threader)
t.daemon = True
t.start()
for worker in range(1, 500): # an another for loop
q.put(worker)
q.join()
print('Time taken:', time.time() - startTime) # the total time taken
And the output looks something like this ...
Enter the host to be scanned: google.com
Starting scan on host: 216.58.200.142
80 is open
443 is open
Time taken: 1.3211915493011475# as we know GOOGLE is a SEARCH ENGINE and PORTS 80 and 443 are the most common ports open ina website...
An another example ...
Enter the host to be scanned: amazon.com
Starting scan on host: 205.251.242.103
80 is open
Time taken: 1.3240206241607666

Comments
Post a Comment