Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old October 20th, 2003, 12:40 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
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.

Reply With Quote
  #2  
Old October 20th, 2003, 02:12 PM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
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.

Reply With Quote
  #3  
Old October 20th, 2003, 02:36 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
*stops banging head on the wall for a moment*

Right.

*resumes banging of head*

Reply With Quote
  #4  
Old October 20th, 2003, 03:40 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #5  
Old October 20th, 2003, 10:46 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
Yeah, I got that. It works now, with that and a change or two. Thanks for the help boys. My forehead still hurts, though.

Reply With Quote
  #6  
Old October 21st, 2003, 08:02 AM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
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.)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Threading problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway