本文實例為大家分享了Python threading模塊對單個接口進行并發測試的具體代碼,供大家參考,具體內容如下
本文知識點
通過在threading.Thread繼承類中重寫run()方法實現定制輸出結果
代碼 如下
import requests
import threading
import sys, io
# 解決console顯示亂碼的編碼問題
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
class Mythread(threading.Thread):
"""This class customizes the output thu overriding the run() method"""
def __init__(self, obj):
super(Mythread, self).__init__()
self.obj = obj
def run(self):
ret = self.obj.test_search_tags_movie()
print('result--%s:\n%s' % (self.getName(), ret))
class Douban(object):
"""A class containing interface test method of Douban object"""
def __init__(self):
self.host = 'movie.douban.com'
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
'Referer':'https://movie.douban.com/',
}
def get_response(self, url, data):
resp = requests.post(url=url, data=data, headers=self.headers).content.decode('utf-8')
return resp
def test_search_tags_movie(self):
method = 'search_tags'
url = 'https://%s/j/%s' % (self.host, method)
post_data = {
'type':'movie',
'source':'index'
}
resp = self.get_response(url=url, data=post_data)
return resp
if __name__ == '__main__':
douban = Douban()
thds = []
for i in range(9):
thd = Mythread(douban)
thd.start()
thds.append(thd)
for thd in thds:
thd.join()
運行結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

