[Tutor] Port scanner (was: tuples and mysqldb tables)

Kent Johnson kent37 at tds.net
Mon Sep 19 20:35:48 CEST 2005


Danny Yoo wrote:
> [Again forwarding to tutor.  Ed, please keep tutor at python.org in CC;
> otherwise, your questions might lose themselves in my mailbox.  I don't
> want to be a bottleneck in your learning.]
> 
> ---------- Forwarded message ----------
> Date: Mon, 19 Sep 2005 00:06:13 -0400
> From: Ed Hotchkiss <edhotchkiss at gmail.com>
> To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
> Subject: Re: [Tutor] tuples and mysqldb tables
> 
> Thanks for the debugging help :P - I've edited the error handling line, and
> defined the port_counter above, but I am still not getting any output. I
> have inserted print "scanned: ",port_counter,"\n"
> into the thread, so that should at least print to screen each time the
> thread is ran, regardless of the port being open or closed. Any ideas why
> the thread itself though is not working?

Do you get an error message printed? If so, it's helpful if you copy and paste the exact traceback into your email.

> A cleaner view of the code is here:
> http://deadbeefbabe.org/paste/1672?_nevow_carryover_=1127102735.7127.0.0.10.809800804887

You still have not defined port_counter in the scope of class scanThread. As Danny suggests, you should pass this value to the scanThread constructor and save it as an instance variable, then use the saved value in scanThread.run().

You might want to try making a non-threaded version of this program first - maybe you could write a function that tests a single port. Once you have that working then you can learn how to call it in a new thread.

Kent

>  Thanks again for the error handling help!
> 
>  On 9/18/05, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
> 
>>
>>
>>>def run(self):
>>>try:
>>>ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>>ss.connect((ip, port_counter))
>>>print "%s | %d OPEN" % (ip, port_counter)
>>>ss.close()
>>>except: pass
>>
>>^^^^
>>
>>Hi Ed,
>>
>>Yikes. Don't do that. *grin*
>>
>>By "that", I mean setting the exception handler here to do nothing.
>>Exception handling shouldn't abused to silence errors like that, at least,
>>not wholesale like that. At the very least, while we're debugging this,
>>import the 'traceback' module and at least give a heads up, like this:
>>
>>except:
>>traceback.print_exc()
>>
>>Once this is in place, expect to see errors. I know what you meant to do:
>>you wanted to ignore network errors, so try to make the exception handler
>>a little more specific in the exceptions it's trying to silence.
>>
>>The problem here is that the handler has been inadvertantely silencing a
>>legitimate NameError. From casual inspection, it's not clear what
>>'port_counter' is. I suspect you want to add that as part of the thread's
>>state, in which case consider adding it within your thread's __init__
>>method.
>>
>>
>>
>>>def scan(ip, begin, end):
>>>for port_counter in range(begin, end):
>>>while threading < MAX_THREADS:
>>>scanThread().start()
>>># end function -------------------
>>
>>
>>Ok, I definitely suspect port_counter now as the culprit here. Make sure
>>you create each thread with port_counter as a part of each thread's state.
>>
>>
>>[Aside: are you trying to a port scanner in the style that Jacob Matthews
>>wrote about here?
>>
>>http://www.kuro5hin.org/story/2004/3/17/93442/8657
>>
>>Just curious.]
>>
>>
>>
>>Good luck to you!
>>
>>
> 
> 
> 



More information about the Tutor mailing list