fork download
  1. #!/usr/bin/env python
  2.  
  3. from multiprocessing import *
  4.  
  5. import sqlite3
  6. import nmap
  7. import time
  8.  
  9. def scanner (s_arg):
  10. nm = nmap.PortScanner ()
  11. nm.scan (hosts=s_arg, arguments="-n -sS -T4 -p 80,81,1080,1081,3128,8080,8081")
  12. for host in nm.all_hosts ():
  13. for proto in nm[host].all_protocols ():
  14. for port in nm[host][proto].keys ():
  15. if nm[host][proto][port]['state'] == 'open':
  16. queue.put ([host, port])
  17.  
  18. def updater (db_cursor):
  19. while True:
  20. u_arg = queue.get ()
  21. if u_arg:
  22. db_cursor.execute ('INSERT INTO open VALUES (?,?,?)', (u_arg[0], u_arg[1], int(time.time())))
  23. #added a commit here
  24. db_cursor.connection.commit()
  25. else:
  26. break
  27.  
  28. sqlite3.enable_callback_tracebacks (True)
  29. conn = sqlite3.connect ('proxy.db')
  30. #db = conn.cursor () #gone to process creation
  31.  
  32. networks = []
  33. for row in db.execute ('SELECT network FROM cidr WHERE country in (\'CN\')'):
  34. networks.append (row[0])
  35.  
  36. queue = Queue ()
  37.  
  38. #new cursor for each process
  39. updater_p = Process (target = updater, args=(conn.cursor ()))
  40. updater_p.daemon = True
  41. updater_p.start ()
  42.  
  43. pool = Pool (5)
  44. pool.map (scanner, networks)
  45. pool.close ()
  46. pool.join ()
  47.  
  48. queue.put ([])
  49. updater_p.join ()
  50.  
  51. conn.commit ()
  52. conn.close ()
  53.  
Runtime error #stdin #stdout #stderr 0.02s 10520KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 6, in <module>
ImportError: No module named nmap