python多線程掃描端口(線程池)
更新時(shí)間:2019年09月04日 14:38:08 作者:guntm124
這篇文章主要為大家詳細(xì)介紹了python多線程掃描端口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
掃描服務(wù)器ip開放端口,用線程池ThreadPoolExecutor,i7的cpu可以開到600個(gè)左右現(xiàn)成,大概20s左右掃描完65535個(gè)端口,根據(jù)電腦配置適當(dāng)降低線程數(shù)
#!/usr/local/python3.6.3/bin/python3.6 # coding = utf-8 import socket import datetime import re from concurrent.futures import ThreadPoolExecutor, wait DEBUG = False # 判斷ip地址輸入是否符合規(guī)范 def check_ip(ipAddr): compile_ip = re.compile('^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$') if compile_ip.match(ipAddr): return True else: return False # 掃描端口程序 def portscan(ip, port): try: s = socket.socket() s.settimeout(0.2) s.connect((ip, port)) openstr = f'[+] {ip} port:{port} open' print(openstr) except Exception as e: if DEBUG is True: print(ip + str(port) + str(e)) else: return f'[+] {ip} port:{port} error' finally: s.close #主程序,利用ThreadPoolExecutor創(chuàng)建600個(gè)線程同時(shí)掃描端口 def main(): while True: ip = input("請(qǐng)輸入ip地址:") if check_ip(ip): start_time = datetime.datetime.now() executor = ThreadPoolExecutor(max_workers=600) t = [executor.submit(portscan, ip, n) for n in range(1, 65536)] if wait(t, return_when='ALL_COMPLETED'): end_time = datetime.datetime.now() print("掃描完成,用時(shí):", (end_time - start_time).seconds) break if __name__ == '__main__': main()
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python 如何創(chuàng)建一個(gè)線程池
- python線程池如何使用
- 解決python ThreadPoolExecutor 線程池中的異常捕獲問題
- Python 使用threading+Queue實(shí)現(xiàn)線程池示例
- python Event事件、進(jìn)程池與線程池、協(xié)程解析
- Python 線程池用法簡單示例
- 詳解python中的線程與線程池
- python自定義線程池控制線程數(shù)量的示例
- Python mutiprocessing多線程池pool操作示例
- Python線程池模塊ThreadPoolExecutor用法分析
- 實(shí)例代碼講解Python 線程池

linux環(huán)境部署清華大學(xué)大模型最新版 chatglm2-6b 圖文教程
這篇文章主要介紹了linux環(huán)境部署清華大學(xué)大模型最新版 chatglm2-6b ,結(jié)合實(shí)例形式詳細(xì)分析了Linux環(huán)境下chatglm2-6b部署相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下
2023-07-07 
pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作
這篇文章主要介紹了pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
2020-06-06 
django框架模板中定義變量(set variable in django template)的方法分析
這篇文章主要介紹了django框架模板中定義變量(set variable in django template)的方法,結(jié)合實(shí)例形式分析了Django框架實(shí)現(xiàn)模板中定義變量與變量賦值相關(guān)操作技巧,需要的朋友可以參考下
2019-06-06