#1
  1. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    11

    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 ?
    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
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Tucson AZ
    Posts
    29
    Rep Power
    0
    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.
  4. #3
  5. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    11
    *stops banging head on the wall for a moment*

    Right.

    *resumes banging of head*
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  8. #5
  9. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    11
    Yeah, I got that. It works now, with that and a change or two. Thanks for the help boys. My forehead still hurts, though.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Tucson AZ
    Posts
    29
    Rep Power
    0
    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.)

IMN logo majestic logo threadwatch logo seochat tools logo