執行緒內有的時候 print 2.2 后 2.2OK沒有輸出,卡住了。之前是因為開了300個執行緒發現速度越來越慢我開了一個執行緒發現的,而且卡住3分鐘左右就又正常了,我把卡住的IP重新保存到一個新的txt在試沒問題,不知道為什么,求解答

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# pip install tqdm
import sys
import time
import socket
import threading
import queue
from tqdm import tqdm
print('[!] versions:1.8')
if len(sys.argv) < 5:
print('[!] Usage: <INPUT> <OUTPUT> <PROTOCOL> <THREADS> <BYTES times>')
sys.exit()
ssdp = b'\x4D\x2D\x53\x45\x41\x52\x43\x48\x20\x2A\x20\x48\x54\x54\x50\x2F\x31\x2E\x31\x0D\x0A\x48\x6F\x73\x74\x3A\x32\x33\x39\x2E\x32\x35\x35\x2E\x32\x35\x35\x2E\x32\x35\x30\x3A\x31\x39\x30\x30\x0D\x0A\x53\x54\x3A\x73\x73\x64\x70\x3A\x61\x6C\x6C\x0D\x0A\x4D\x61\x6E\x3A\x22\x73\x73\x64\x70\x3A\x64\x69\x73\x63\x6F\x76\x65\x72\x22\x0D\x0A\x4D\x58\x3A\x33\x0D\x0A\x0D\x0A'
input_file = sys.argv[1] # Input
out_file = sys.argv[2] # Output
proto = sys.argv[3]
number_threads = int(sys.argv[4])
if proto == 'ssdp':
type = 3
port = 1900
payload = ssdp
else:
print('[!] Protocol is not available')
sys.exit()
minimum_byte = (int(sys.argv[5]))
class myThread(threading.Thread):
def __init__(self, threadID):
threading.Thread.__init__(self)
self.threadID = threadID
def run(self):
get_info()
ok_ips = []
def get_info():
global out_file
global minimum_byte
global ok_ips
global port
global type
bbyte = 0
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0.5)
while not workQueue.empty():
try:
queueLock.acquire()
if workQueue.qsize() == 0:
continue
ip = workQueue.get()
queueLock.release()
if ip not in ok_ips:
queueLock.acquire()
ok_ips.append(ip)
queueLock.release()
s.sendto(payload, (ip, port))
dataLen = 0
data, addr = s.recvfrom(65500)
dataLen += len(data)
start=time.time()#開始計時
while True:
try:
data, addr = s.recvfrom(65500)
dataLen += len(data)
end=time.time()
if int((end-start)) > 1:
raise Exception('start out')#手動拋出例外
except Exception as b:
break
bbyte = int((dataLen/len(payload)))
if bbyte >= minimum_byte:
queueLock.acquire()
with open(out_file, 'a+', encoding='utf8') as f:
f.write(str(ip) + " " + str(type) + " " + str(bbyte) + '\n')
queueLock.release()
except Exception as b:
pass
s.close()
queueLock = threading.Lock() # 行程鎖
workQueue = queue.Queue() # 作業佇列
threads = []
# 填充佇列
ips = open(input_file, 'r', encoding='utf8')
queueLock.acquire()
for ip in ips:
workQueue.put(str(ip).strip())
queueLock.release()
ips.close()
start_size = workQueue.qsize()
# 創建新執行緒
for tName in range(number_threads):
thread = myThread(tName)
thread.start()
threads.append(thread)
# 等待佇列清空
pbar = tqdm(total=start_size)
while not workQueue.empty():
pbar.n = start_size - workQueue.qsize()
pbar.refresh()
# 等待所有執行緒完成
for t in threads:
t.join()
# 記錄資料
print('執行完畢' )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/107901.html
標籤:其他開發語言
