Python prepared by a multi-threaded port scanning code

From , 3 Years ago, written in Python, viewed 51 times.
URL https://pastebin.vip/view/518a38cc
  1. #!/usr/bin/env python
  2. import socket
  3. import sys
  4. import threading
  5. import time
  6.  
  7. NORMAL = 0
  8. ERROR = 1
  9. TIMEOUT = 5
  10.  
  11. def ping(ip , port , timeout=TIMEOUT):
  12.         try:
  13.                 cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  14.                 cs.settimeout(float(timeout))
  15.                 address=(str(ip),int(port))
  16.                 status = cs.connect_ex((address))
  17.                 if status == NORMAL :
  18.                         print "%d is NORMAL" %port
  19.         except Exception ,e:
  20.                 print ERROR
  21.                 print "error:%s" %e
  22.                 return ERROR
  23.         cs.close()
  24.         return NORMAL
  25.  
  26.  
  27. class Scan(threading.Thread):
  28.     def __init__(self , ip , timeout):
  29.         threading.Thread.__init__(self)
  30.         self.ip = ip
  31.         self.timeout = timeout
  32.  
  33.     def run(self):
  34.         global p_begin , p_end , mutex
  35.         threadname = threading.currentThread().getName()
  36.         while 1:
  37.             mutex.acquire()
  38.             p_begin += 1
  39.             if p_begin >  p_end:
  40.                     mutex.release()
  41.                     break
  42.             mutex.release()
  43.             ping(self.ip , p_begin , self.timeout)
  44.  
  45.        
  46.  
  47. if __name__ == '__main__':
  48.     if len(sys.argv) < 4:
  49.         print 'format:urlbegin portbegin portend timeout'
  50.  
  51.     urlbegin = str(sys.argv[1])
  52.     portbegin = int(sys.argv[2])
  53.     portend = int(sys.argv[3])
  54.     timeout = float(sys.argv[4])
  55.  
  56.     global p_begin , p_end, mutex
  57.     threads = []
  58.     num = 10
  59.     p_begin = portbegin
  60.     p_end = portend
  61.     mutex = threading.Lock()
  62.     for x in xrange(0 , num):
  63.         t_scan = Scan(urlbegin , timeout)
  64.         t_scan.setDaemon(True)
  65.         threads.append(t_scan)
  66.     for t in threads:
  67.         t.start()
  68.     for t in threads:
  69.         t.join()  
  70.  
  71. #//python/8926

Reply to "Python prepared by a multi-threaded port scanning code"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website