本文知识点
利用gevent模块进行并发测试
代码如下
1 from gevent import monkey 2 monkey.patch_all() 3 import requests 4 import gevent 5 import sys, io 6 # 解决console显示乱码的编码问题 7 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 8 9 10 class Douban(object): 11 """A class containing interface test method of Douban object""" 12 def __init__(self): 13 self.host = 'movie.douban.com' 14 self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0', 15 'Referer':'https://movie.douban.com/', 16 } 17 18 def get_response(self, url, data): 19 resp = requests.post(url=url, data=data, headers=self.headers).content.decode('utf-8') 20 return resp 21 22 def test_search_tags_movie(self): 23 method = 'search_tags' 24 url = 'https://%s/j/%s' % (self.host, method) 25 post_data = { 26 'type':'movie', 27 'source':'index' 28 } 29 resp = self.get_response(url=url, data=post_data) 30 print(resp) 31 return resp 32 33 if __name__ == '__main__': 34 douban = Douban() 35 threads = [] 36 for i in range(6): 37 thread = gevent.spawn(douban.test_search_tags_movie) 38 threads.append(thread) 39 40 gevent.joinall(threads)
代码运行结果