python 實(shí)現(xiàn)端口掃描工具
更新時(shí)間:2020年12月18日 09:51:53 作者:lucky_qi
這篇文章主要介紹了python 實(shí)現(xiàn)端口掃描工具的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
# 簡單的端口掃描工具
# 作者: Charles
# 公眾號(hào): Charles的皮卡丘
import time
import socket
import threading
# 判斷是否為正確的IP地址。
def isIP(ip):
ip_addr = ip.split('.')
if len(ip_addr) != 4:
return False
for ipnum in ip_addr:
if not (0 <= int(ipnum) < 255):
return False
else:
return True
# 端口掃描工具
class scanThread(threading.Thread):
def __init__(self, ip, port_min=0, port_max=65535):
# 初始化。
threading.Thread.__init__(self)
self.port_max = port_max
self.port_min = port_min
self.ip = ip
# assert isinstance(int,self.port_min) and isinstance(int,self.port_max)
# 重寫run
def run(self):
return self.__checker()
# 檢測
def __checker(self):
for port in range(self.port_min,self.port_max):
self.__connect(port)
# 連接
def __connect(self,port):
socket.setdefaulttimeout(1)
self.sock = socket.socket()
try:
start_time = time.time()
self.sock.connect((self.ip,port))
end_time = time.time()
connect_time = int(start_time - end_time)
info = 'Find --> [IP]: %s, [PORT]: %s, [Connect Time]: %d' % (self.ip, port, connect_time)
print(info)
self.__save(info)
self.sock.close()
except:
# print('出錯(cuò)誤了')
self.sock.close()
def __save(self,info):
try:
with open('results.txt', 'a') as f:
f.write(info + '\n')
except:
print('寫文件出現(xiàn)了問題')
time.sleep(0.1)
if __name__ == '__main__':
# 輸入IP地址。
ip = input('Input IP(example <xxx.xxx.xxx.xxx>):\n')
print(isIP(ip))
while not isIP(ip):
ip = input('請(qǐng)輸入正確的IP地址:\n')
# 輸入最小端口、
port_min = input('需要掃描的最小端口為:')
while not (0 <= int(port_min) < 65535):
port_min = input('請(qǐng)輸入正確的需要掃描的最小端口:')
port_max = input('需要掃描的最大端口為(65535):')
while not (0 <= int(port_min) < int(port_max) < 65535):
port_min = input('請(qǐng)輸入正確的需要掃描的最大端口(65535):')
num = 8
port_max = int(port_max)
port_min = int(port_min)
interval = (port_max - port_min) // num
for i in range(interval):
scanThread(ip, i * num, (i + 1) * num).start()
以上就是python 實(shí)現(xiàn)端口掃描工具的詳細(xì)內(nèi)容,更多關(guān)于python 端口掃描工具的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
夯實(shí)基礎(chǔ)python集合的應(yīng)用場景及字符串定義和表示
這篇文章主要為大家介紹了python集合的應(yīng)用場景及字符串定義和表示,來幫大家夯實(shí)基礎(chǔ),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-10-10
深入解析Python編程中super關(guān)鍵字的用法
Python的子類調(diào)用父類成員時(shí)可以用到super關(guān)鍵字,初始化時(shí)需要注意super()和__init__()的區(qū)別,下面我們就來深入解析Python編程中super關(guān)鍵字的用法:2016-06-06
解決Numpy與Pytorch彼此轉(zhuǎn)換時(shí)的坑
這篇文章主要介紹了解決Numpy與Pytorch彼此轉(zhuǎn)換時(shí)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
PyTorch中torch.tensor與torch.Tensor的區(qū)別詳解
這篇文章主要介紹了PyTorch中torch.tensor與torch.Tensor的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

