|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
Threading problem
Hey all. I've been working on the following for some time now as an exercise :
Code:
import socket,thread
def scan_port(ip,port,s):
global openports,t_number,t_lock,open_lock
if not s.connect_ex((ip, n)):
open_lock.acquire()
openports.append(n)
open_lock.release()
t_lock.acquire()
thread_number -= 1
t_lock.release()
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):
thread.start_new_thread(scan_port,(ip,n,sockets[n-Sport]))
while t_number > 0: pass
if len(openports) > 0: print "Open ports: ",openports
else: print "No open ports. Sorry."
for s in sockets:
s.close()
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. 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 ?
__________________
Time is the greatest of teachers ; sadly, it kills all of its students. - Hector Berlioz Last edited by SolarBear : October 20th, 2003 at 02:20 PM. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
*stops banging head on the wall for a moment*
Right. *resumes banging of head* |
|
#4
|
||||
|
||||
|
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, Mark. |
|
#5
|
||||
|
||||
|
Yeah, I got that. It works now, with that and a change or two. Thanks for the help boys. My forehead still hurts, though.
|
|
#6
|
|||
|
|||
|
Solar,
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.) |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Threading problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|