Hello all. I been messing around with this port scanner for a while now and can't wrap my head around the speed issue. I tried to create a few threads to help speed it up, but I'm pretty sure I havn't made any progress. Could someone explain why the speed is so slow, and why the threads I created still run in succesion and not at the same time to speed the process up. Any help or suggestions would be greatly appreciated.
import socket
from threading import Thread
class scanner(object):
address = "127.0.0.1"
port = 0
def __init__(self):
scan1 = Thread(None, self.r1to100())
scan1.start()
scan2 = Thread(None, self.r100to200())
scan2.start()
def r1to100(self):
for x in range(1, 100):
print("checking port " + str(x) + " on " + self.address)
check = self.scan_server(self.address, x)
if check == True:
print("scan_server returned " + str(check))
print()
else:
pass
def r100to200(self):
for x in range(100, 200):
print("checking port " + str(x) + " on " + self.address)
check = self.scan_server2(self.address, x)
if check == True:
print("scan_server returned " + str(check))
print()
else:
pass
def scan_server(self, address, port):
s = socket.socket()
#print("Attempting to connect to " + address + " on port " + str(port))
try:
s.connect((address, port))
print("Connected to server " + address + " on self.port " + str(port))
return True
except socket.error:
#print("Connecting to " + address + " on port " + str(port) + " failed")
return False
def scan_server2(self, address, port):
s = socket.socket()
#print("Attempting to connect to " + address + " on port " + str(port))
try:
s.connect((address, port))
print("Connected to server " + address + " on port " + str(port))
return True
except socket.error:
#print("Connecting to " + address + " on port " + str(port) + " failed")
return False
scan = scanner()