python實現(xiàn)的多線程端口掃描功能示例
本文實例講述了python實現(xiàn)的多線程端口掃描功能。分享給大家供大家參考,具體如下:
下面的程序給出了對給定的ip主機進行多線程掃描的Python代碼
#!/usr/bin/env python
#encoding: utf-8
import socket, sys, thread, time
openPortNum = 0
socket.setdefaulttimeout(3)
def usage():
print '''''Usage:
Scan the port of one IP: python port_scan_multithread.py -o <ip>
Scan the port of one IP: python port_scan_multithread.py -m <ip1, ip2, ip3, ip4 ...>
'''
print 'Exit'
sys.exit(1)
def socket_port(ip, PORT):
global openPortNum
if PORT > 65535:
print 'Port scanning beyond the port range, interrupt to scan'
sys.exit(1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex((ip, PORT))
if(result == 0):
print ip, PORT,'is open'
openPortNum += 1
s.close()
def start_scan(IP):
for port in range(0, 65535+1):
thread.start_new_thread(socket_port, (IP, int(port)))
time.sleep(0.006)
if __name__ == '__main__':
t = 0
if len(sys.argv)<2 or sys.argv[1] == '-h':
usage()
elif sys.argv[1] == '-o':
ONE_IP = raw_input('Please input ip of scanning: ')
t = time.time()
start_scan(ONE_IP)
elif sys.argv[1] == '-m':
MANY_IP = raw_input('Please input many ip of scanning: ')
IP_SEG = MANY_IP.split(',')
t = time.time()
for i in IP_SEG:
start_scan(i)
print
print 'total open port is %s, scan used time is: %f ' % (openPortNum, time.time()-t)
運行效果圖

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python URL操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python3.4 tkinter,PIL圖片轉(zhuǎn)換
我們給大家整理了關(guān)于Python3.4 tkinter,PIL圖片轉(zhuǎn)換的相關(guān)完整代碼,大家可以學(xué)習(xí)測試下。2018-06-06
在linux系統(tǒng)中安裝python3.8.1?并卸載?python3.6.2?更新python3引導(dǎo)到3.8.1的
這篇文章主要介紹了如何在linux系統(tǒng)中安裝python3.8.1?并卸載?python3.6.2?更新python3引導(dǎo)到3.8.1,本文分步驟給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11

