python多線程掃描端口(線程池)
掃描服務(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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python 如何創(chuàng)建一個(gè)線程池
- python線程池如何使用
- 解決python ThreadPoolExecutor 線程池中的異常捕獲問題
- Python 使用threading+Queue實(shí)現(xiàn)線程池示例
- python Event事件、進(jìn)程池與線程池、協(xié)程解析
- Python 線程池用法簡(jiǎn)單示例
- 詳解python中的線程與線程池
- python自定義線程池控制線程數(shù)量的示例
- Python mutiprocessing多線程池pool操作示例
- Python線程池模塊ThreadPoolExecutor用法分析
- 實(shí)例代碼講解Python 線程池
相關(guān)文章
Python調(diào)用騰訊API進(jìn)行人像動(dòng)漫化效果實(shí)例
最近上網(wǎng)的時(shí)候看到了一個(gè)有趣的東西,叫做人物動(dòng)漫化,嘗試著用python實(shí)現(xiàn)了,所以下面這篇文章主要給大家介紹了關(guān)于Python調(diào)用騰訊API進(jìn)行人像動(dòng)漫化效果的相關(guān)資料,需要的朋友可以參考下2023-06-06
python3.6使用pymysql連接Mysql數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了python3.6使用pymysql連接Mysql數(shù)據(jù)庫(kù),以及簡(jiǎn)單的增刪改查操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
python中ASCII碼字符與int之間的轉(zhuǎn)換方法
今天小編就為大家分享一篇python中ASCII碼字符與int之間的轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python爬蟲分布式獲取數(shù)據(jù)的實(shí)例方法
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python爬蟲分布式獲取數(shù)據(jù)的實(shí)例方法,有興趣的朋友們可以參考下。2020-11-11
Pycharm如何設(shè)置默認(rèn)請(qǐng)求頭和切換python環(huán)境
這篇文章主要介紹了Pycharm如何設(shè)置默認(rèn)請(qǐng)求頭和切換python環(huán)境問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
python如何處理matlab的mat數(shù)據(jù)
這篇文章主要介紹了python如何處理matlab的mat數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

