Original Post
Using ref: http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf I'm trying to learn multi-threading. I tried to understand threading by writing a simple portscanner. ( uses 'ping' on each CPU on the LAN ) (1) Is there a max_limit to how many threads can be alive at once? Because if I scan 50 IP's its safe. But range: 1-80 and it makes a lot of dings. ( seems to be on calls to system("ping"), but not every call. seems random ) Then I tried 1-255, and it still dings. And sometimes ( not always ) it makes 1 or more ( sometimes alot ) of dialogs, which say: "application falied to initialize properly(0xc0000142) Click on OK to terminate the application." (2) if there is a limit, how do I wait untill a slot is free in thread_list, and use it? (3) The output text scrolls fast and fills the limit of cmd.exe's buffer, so I can't find the exact text. I think sometimes the code says something like "unhandled exception " ( related to threads ) but it doesn't end the program like a normal unhandled exception does! (4) How do you know if an operation requires locking? I would have thought 'a += b' would be safe, but the tutorial says its not. If assignment isn't safe, is anything safe? ( only operations that are read only? ) (5) Sometimes garbage in STDOUT. ( maybe related to #1,#2, or #3 above ) Garbage below shown as '?'. Here's my code:
Pinging 192.168.255.7 with 32 bytes of data:
Pinging 192.168.255.8 with 32 bytes of data:
???Pinging 192.168.255.9 with 32 bytes of data:??????
Pinging 192.168.255.10 with 32 bytes of data:
Pinging 192.168.255.11 with 32 bytes of data:
import os,sys
import thread
import threading
import time
print """----------------------
attempt at: multi-threaded basic portscanner. ( not really portscanner, just
does a ping to see what my LAN cpu's IP's are. )
Seems to be working, except for (1)strange ding,(2) possible unhanlded exception
and (3)dialog error :(
usage:
"""
class Log():
def __init__(self, file):
self.log_lock = thread.allocate_lock()
self.log = open(file, "a")
self.write("== %s/%s/%s %s:%s:%s ==\n" % time.localtime()[0:6])
def write(self, str):
"""thread safe writing ( i *think* ).
not sure if in this case ( file appending) if locking is required?"""
self.log_lock.acquire()
self.log.write("%s\n" % str)
self.log_lock.release()
class ScanAddr( threading.Thread ):
def __init__( self, ip,cur,log ):
self.ip = ip
self.cur = cur
self.log = log
threading.Thread.__init__(self)
def run( self ):
res = os.system("ping %s.%s -n 1" % (self.ip, self.cur ))
# lock and log
if res == 0:
print "%s.%s is online" % (self.ip, self.cur)
self.log.write("%s.%s is online" % (self.ip, self.cur) )
elif res == 1:
print "%s.%s is offline, or ping request blocked." % (self.ip, self.cur)
ip = sys.argv[1]
start = int(sys.argv[2])
end = int(sys.argv[3])
cur = start
# init: locks:
cur_lock = thread.allocate_lock()
# init: log
log = Log("05.log.txt")
log.write("scanning: %s.%s-%s" % (ip, start, end ))
thread_list = [] # holds instances of ScanAddr(...), never gets cleared!
while cur<end:
# do scan:
cur_thread = ScanAddr( ip,cur,log )
cur_thread.start()
thread_list.append( cur_thread )
# don't think know if I actually need to lock cur inc. or not.
# Tut says "I do"
cur_lock.acquire()
cur += 1
cur_lock.release()