October 20th, 2003, 12:40 PM
Hey all. I've been working on the following for some time now as an exercise :
So that's my code. Basically, it's a threaded port scanner. I thought the threading was necessary because the first version took over 30 seconds to scan a mere 100 ports.
if not s.connect_ex((ip, n)):
thread_number -= 1
ip = '127.0.0.1'
Sport = input("Please enter the start port number: ")
Eport = input("Please enter the end port number: ")
Eport = Eport + 1
print "You are scanning", ip
print "for ports", Sport,"to", Eport - 1
openports = 
sockets = [socket.socket(socket.AF_INET,socket.SOCK_STREAM) for i in range(Sport,Eport)]
open_lock = thread.allocate_lock()
t_number = Eport - Sport + 1
t_lock = thread.allocate_lock()
for n in range(Sport, Eport):
while t_number > 0: pass
if len(openports) > 0: print "Open ports: ",openports
else: print "No open ports. Sorry."
for s in sockets:
The problem is : I get the following error : Unhandled exception in thread started by ... and that's it. After receiving that message, the interpreted freezes, which isn't really helpful in debugging.
Any idea of what's wrong, anybody ?
Last edited by SolarBear; October 20th, 2003 at 02:20 PM.
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz
October 20th, 2003, 02:12 PM
When I ran your program I recieved the following error:
Unhandled exception in thread started by <function scan_port at 0x008EC9B0>
Traceback (most recent call last):
File "portscan.py", line 10, in scan_port
thread_number -= 1
UnboundLocalError: local variable 'thread_number' referenced before assignment
The last line indicates the problem
in line 10 of your code you are setting 'thread_number -=1'
however thread_number has not been assigned a value yet.
I believe this should be t_number?
Last edited by irishtek; October 20th, 2003 at 02:16 PM.
October 20th, 2003, 02:36 PM
*stops banging head on the wall for a moment*
*resumes banging of head*
October 20th, 2003, 03:40 PM
I think what irish is trying to say in his own.. overly comfusing way is that thread_number only comes up once in your program (on line 10). rename 'thread_number' to 't_number' and try again
Take care solar,
October 20th, 2003, 10:46 PM
Yeah, I got that. It works now, with that and a change or two. Thanks for the help boys. My forehead still hurts, though.
October 21st, 2003, 08:02 AM
I never did understand the point of those wristpads placed in front of the keyboard, until after I started programming.
(They're much softer on the head.)