Home > other >  Python sendto stuck problem, ask for help
Python sendto stuck problem, ask for help

Time:10-03

Sometimes print 2.2 within the thread after 2.2 OK no output, stuck, because before opened 300 threads found more and more slowly I found opened a thread, and normal again stuck 3 minutes or so, I've stuck to save to a new IP TXT in test no problem, I don't know why, please answer



 #! The/usr/bin/env python 
# - * - coding: utf-8 - * -
# PIP install TQDM
The import sys
The import time
The import socket
The import threading
The import queue
The from TQDM import TQDM

Print (' [!] Versions: 1.8 ')
If len (sys. Argv) & lt; 5:
Print (' [!] Usage: & lt; INPUT> ')
Sys. The 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
Content=SSDP
The else:
Print (' [!] Protocol is not available ')
Sys. The exit ()

Minimum_byte=(int (sys. Argv [5]))
Class myThread (threading. Thread) :
Def __init__ (self, threadID) :
Threading. Thread. __init__ (self)
The self. The 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. The socket (socket. AF_INET, socket. SOCK_DGRAM)
S.s ettimeout (0.5)
While not workQueue. Empty () :
Try:
QueueLock. Acquire ()
If workQueue. Qsize ()==0:
The continue
IP=workQueue. The get ()
QueueLock. Release ()
If IP not in ok_ips:
QueueLock. Acquire ()
Ok_ips. Append (IP)
QueueLock. Release ()
S.s endto (payload, (IP, port))
DataLen=0
Data, addr=s.r ecvfrom (65500)
DataLen +=len (data)
Timing starts start=time. Time () #
While True:
Try:
Data, addr=s.r ecvfrom (65500)
DataLen +=len (data)
End=time. Time ()
If the int ((end - start)) & gt; 1:
Raise the Exception (' start out ') # manual throws an Exception
Except the Exception as b:
Break
Bbyte=int ((dataLen/len (content))
If bbyte & gt;=minimum_byte:
QueueLock. Acquire ()
With the open (out_file, 'a +' encoding='utf8') as f:
F.w rite (STR + "" + STR (IP) (type) +" "+ STR (bbyte) + '\ n')
QueueLock. Release ()
Except the Exception as b:
Pass
S.c lose ()

QueueLock=threading. Lock Lock () # process
WorkQueue=queue. The queue () # work queue
Threads=[]

# fill the queue
Ips=open (input_file, 'r', encoding='utf8')
QueueLock. Acquire ()
For IP in ips:
WorkQueue. Put (STR (IP). Strip ())
QueueLock. Release ()
Ips. The close ()
Start_size=workQueue. Qsize ()

# to create a new thread
For tName in range (number_threads) :
Thread=myThread (tName)
Thread. The start ()
Threads. Append (thread)

# wait queue to empty
Pbar=TQDM (total=start_size)
While not workQueue. Empty () :
Pbar. N=start_size - workQueue. Qsize ()
Pbar. Refresh ()

# all threads waiting for complete
For t in threads:
T.j oin ()

To record data #
Print (' complete ')
  • Related