1. 程式人生 > >python多執行緒的學習

python多執行緒的學習

0x00.前言

  學了一下python的多執行緒,threading模組

  感覺挺有意思的,隨便練手寫了一個很粗陋的windows下多執行緒掃線上ip的指令碼

  指令碼沒什麼技術含量,純粹練手,掃一趟192的區域網要花個5分多鐘...主要是因為直接用的python呼叫system命令去ping...

0x01.程式碼

# Author: 昏鴉

import os
import threading

def scan(lis):
    ip = "192.168.1.{}"
    for i in lis:
        res = ''.join(os.popen('ping -n 2 
' + ip.format(i)).readlines()) pattern = "([\d]+)% 丟失" drop = re.search(pattern,res).group(1) if '無法訪問' in res: continue else: if int(drop)< 51: print(ip.format(i)) else: continue if __name__ == '
__main__': threads = [] for i in range(5): t = threading.Thread(target=scan,args=(list(range(i*51,i*51+51)),)) threads.append(t) for t in threads: t.start() for t in threads: t.join()