Python+threading模块对单个接口进行并发测试
作者:梦四十九剑 时间:2023-07-07 02:41:41
本文实例为大家分享了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()
运行结果
来源:https://blog.csdn.net/lylfv/article/details/82043487
标签:python,接口,并发测试
0
投稿
猜你喜欢
python浪漫表白源码
2023-11-22 05:16:39
Python中文件遍历的两种方法
2022-01-28 20:34:03
100行Python代码实现每天不同时间段定时给女友发消息
2023-07-11 20:32:56
学会迭代器设计模式,帮你大幅提升python性能
2023-01-22 12:36:01
Django RestFramework 全局异常处理详解
2023-12-15 03:16:42
HTML邮件的又一点思考
2009-05-06 13:33:00
python通配符之glob模块的使用详解
2021-07-16 23:13:18
用Python将动态GIF图片倒放播放的方法
2023-02-17 07:39:51
详解python如何调用C/C++底层库与互相传值
2022-02-25 07:18:00
ASP-server.URLEncode反函数:urldecode
2008-10-23 16:05:00
Python 绘图库 Matplotlib 入门教程
2021-07-21 00:22:07
python 常用的基础函数
2023-07-24 11:10:51
Python运行错误异常代码含义对照表
2023-11-14 09:20:09
用户体验设计何去何从,交互设计师又何去何从?
2009-12-28 13:07:00
python图像填充与裁剪/resize的实现代码
2022-07-12 07:49:15
Python使用mmap实现内存映射文件操作
2022-04-26 14:37:19
基于python实现图片转字符画代码实例
2023-05-17 01:53:37
python实现五子棋双人对弈
2023-11-14 05:36:10
详解python中文编码问题
2023-10-17 12:09:08
php小经验:解析preg_match与preg_match_all 函数
2023-10-31 08:55:23