I. Queue Module Basics
Q = Queue.queue ()
Q.qsize () returns the size of the queue
Q.empty () returns True if the queue is empty, and vice versa false
Q.full () returns True if the queue is full, otherwise false
Q.full corresponds to maxsize size
Q.get ([block[, timeout]]) Get queue, timeout wait time
Q.get_nowait () quite q.get (False)
Q.put (item) write queue, timeout wait time
Q.put_nowait (item) quite Q.put (item, False)
Q.task_done () After completing a work, the Q.task_done () function sends a signal to the queue that the task has completed
Q.join () actually means waiting until the queue is empty before performing another operation
Two. Threading Module Basics
t = Threading. Thread uses it to create threads
T.start () Start the execution of the thread
T.run () contains what the thread actually does
T.join (Timeout=none) The thread that is called by the Join () method will block the caller's thread until it ends
T.name a string used to identify the process
T.getname ()/t.setname () is a method reserved for back-compatibility and is now accessed directly through the Name property
T.ident if the thread has not started, the value is None
T.is_alive () returns whether this process is a alive state
T.daemon Indicates whether this process is "daemon thread". Be sure to set before start () is called, or the RuntimeError will be raised.
T.isdaemon ()/T.setdaemon () method reserved for back compatibility, now direct access to the Daemon property
Lock = Threading. Lock
Lock.acquire (blocking=true,timeout=-1) gets a lock
Lock.release () Release a lock
Three. Multi-threaded Port scanner
#-*-coding:utf-8-*-__author__='Dleo'ImportSYSImportSocketImportThreadingImportQueueports=[21,80,445,3389]queue=Queue.queue ()defIp_c (IP): new_ip= [] forIp_cinchRange (1,255): New_ip.append (str (IP)+"."+str (ip_c))#Print New_ip returnnew_ipclassThreadnum (object):def __init__(self, queue): Self.queue=Queue Self.lock=Threading. Lock ()defPort_open (self): whileTrue:Try: Try: IP= Self.queue.get (timeout=1) except: Break forPortinchPorts:socket.setdefaulttimeout (0.5) s=Socket.socket (socket.af_inet, socket. SOCK_STREAM)ifS.CONNECT_EX ((IP, port)) = =0:self.lock.acquire ()Print(IP +':'+ STR (port) +'is openning') Self.lock.release () s.close ()except: PassSelf.queue.task_done ()defRun (self): TMP= [] forIinchRange (255): T= Threading. Thread (target=self.port_open) T.setdaemon (True) T.start () tmp.append (t) forTinchTmp:t.join ()if __name__=='__main__': ifLen (SYS.ARGV)! = 2: Print "Please put the IP like this ' 127.0.0 '"sys.exit () IP= Sys.argv[1] forIinchIp_c (IP): queue.put (i) t=threadnum (queue) T.run ()
:
Multi-threaded port scanners with threading and queue modules