. 1 Import Requests 2 Import Queue # Queue module provides synchronization, thread-safe queue classes, including . 3 # the FIFO (First In First Out) queue Queue, LIFO (last in, first out) queue . 4 # LifoQueue, and priority queue PriorityQueue . These queues are 5 # implements locking primitives can be used directly in a multi-threaded communications. . 6 Import Threading . 7 Import Time . 8 . 9 status_code_list = [] 10 exec_time = 0 . 11 class MyThreadPool: 12 is DEF the __init__ (Self, MAXSIZE): # there is a default parameter defines the queue 13 #maxsize, if the queue length is not specified, i.e. manxsize = 0, then the queue length 14 # of infinite length, if the defined value greater than 0, then the queue length is maxsize. 15 self.maxsize = MAXSIZE 16 self._pool = Queue.Queue (MAXSIZE) 17 # MAXSIZE set the size of the queue for the pool size 18 for _ in the Range (MAXSIZE): # Why use an underscore, because in fact this 19 # years useless to this variable, so with a symbol on it. 20 self._pool.put (threading.Thread) # into the pool put the number of threads 21 22 DEF get_thread (Self): 23 return self._pool.get () 24- 25 DEF add_thread(self): 26 self._pool.put(threading.Thread) 27 28 def request_time(func): 29 def inner(*args, **kwargs): 30 global exec_time 31 start_time = time.time() 32 func(*args, **kwargs) 33 end_time = time.time() 34 exec_time = end_time-start_time 35 36 return inner 37 38 39 def get_url(url): 40 global x,status_code_list 41 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36', 42 } 43 response = requests.get(url,headers=headers) 44 code = response.status_code 45 status_code_list.append(code) 46 print(code) 47 return code 48 49 50 def get_count(_url='http://news.baidu.com/sports',_count=100): # :param count: 每个线程请求的数量 51 global status_code_list,url,count 52 for i in range(count): 53 get_url(url) 54 55 def request_status(): 56 count_num = len(status_code_list) 57 set_code_list = set(status_code_list) 58 status_dict = {} 59 for i in set_code_list: 60 status_dict[i] = str(status_code_list).count(str(i)) 61 echo_str(count_num, set_code_list, status_dict) 62 63 DEF echo_str (count_num, set_code_list, status_dict): 64 Print ( ' ==================================== === ' ) 65 Print ( ' request total number:% s ' % count_num) 66 Print ( ' length request:% s seconds ' % int (exec_time)) 67 second_request = count_num / int (exec_time) 68 Print ( ' per sec request about:% s times ' % int (second_request)) 69 Print ( ' status code | times ' ) 70 71 is for K, V in status_dict.items (): 72 Print (STR (K) + ' | ' + STR (V)) 73 is Print ( ' ================= ====================== ' ) 74 75 76 @request_time 77 DEF RUN (URL, thread_num = 10, thread_pool = 10 ): 78 ' '' 79 : param thread_num: (total number of threads executing requests * = total number of requests for each thread loop) total number of threads of execution 80 : param thread_pool: number of thread pool 81 : param URL: domain address request 82 '' ' 83 global x,status_code_list 84 pool = MyThreadPool(thread_pool) 85 for i in range(thread_num): 86 t = pool.get_thread() 87 obj = t(target=get_count) 88 obj.start() 89 obj.join() 90 91 92 if __name__ == '__main__': 93 count = 10 #单个线程的请求数 94 url = 'http://baidu.com' 95 run(url,100,100) 96 request_status()