Python+threading模块对单个接口进行并发测试


Posted in Python onJune 25, 2019

本文实例为大家分享了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()

运行结果

Python+threading模块对单个接口进行并发测试

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python错误处理详解
Sep 28 Python
简介Django框架中可使用的各类缓存
Jul 23 Python
python使用socket创建tcp服务器和客户端
Apr 12 Python
Python拼接微信好友头像大图的实现方法
Aug 01 Python
Python3.5常见内置方法参数用法实例详解
Apr 29 Python
在PyCharm中控制台输出日志分层级分颜色显示的方法
Jul 11 Python
Django模型修改及数据迁移实现解析
Aug 01 Python
Python中输入和输出(打印)数据实例方法
Oct 13 Python
pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率
Jan 02 Python
python中列表的含义及用法
May 26 Python
Python命名空间namespace及作用域原理解析
Jun 05 Python
python百行代码实现汉服圈图片爬取
Nov 23 Python
python简单鼠标自动点击某区域的实例
Jun 25 #Python
详解pandas删除缺失数据(pd.dropna()方法)
Jun 25 #Python
python命令行参数用法实例分析
Jun 25 #Python
PyQt5重写QComboBox的鼠标点击事件方法
Jun 25 #Python
bluepy 一款python封装的BLE利器简单介绍
Jun 25 #Python
PyQt5下拉式复选框QComboCheckBox的实例
Jun 25 #Python
在Pandas中处理NaN值的方法
Jun 25 #Python
You might like
为什么《星际争霸》是测试人工智能的理想战场
2019/12/03 星际争霸
php连接mysql数据库代码
2009/03/10 PHP
PHP 字符串正则替换函数preg_replace使用说明
2011/07/15 PHP
php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
2013/08/10 PHP
PHP批量删除jQuery操作
2017/07/23 PHP
自定义Laravel (monolog)日志位置,并增加请求ID的实现
2019/10/17 PHP
tp5递归 无限级分类详解
2019/10/18 PHP
仅IE不支持setTimeout/setInterval函数的第三个以上参数
2011/05/25 Javascript
js实现目录定位正文示例
2013/11/14 Javascript
Bootstrap学习笔记之css样式设计(1)
2016/06/07 Javascript
基于JS实现checkbox全选功能实例代码
2016/10/31 Javascript
Bootstrap源码解读表单(2)
2016/12/22 Javascript
Bootstrap 过渡效果Transition 模态框(Modal)
2017/03/17 Javascript
使用JavaScript根据图片获取条形码的方法
2017/07/04 Javascript
深入理解ES6学习笔记之块级作用域绑定
2017/08/19 Javascript
详解使用create-react-app快速构建React开发环境
2018/05/16 Javascript
详解angularjs跨页面传参遇到的一些问题
2018/11/01 Javascript
vue中的适配px2rem示例代码
2018/11/19 Javascript
python三元运算符实现方法
2013/12/17 Python
python3解析库pyquery的深入讲解
2018/06/26 Python
python3 对list中每个元素进行处理的方法
2018/06/29 Python
解决python通过cx_Oracle模块连接Oracle乱码的问题
2018/10/18 Python
Python 互换字典的键值对实例
2019/02/12 Python
如何用python批量调整视频声音
2020/12/22 Python
python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)
2021/02/19 Python
windeln官方海外旗舰店:德淘超人气母婴超市
2017/12/15 全球购物
戴尔美国官方折扣店:Dell Outlet
2018/02/13 全球购物
ONLY瑞典官网:世界知名服装品牌
2018/06/19 全球购物
伊莱克斯阿根廷网上商店:Tienda Electrolux
2021/03/08 全球购物
Linux如何修改文件和文件夹的权限
2013/09/05 面试题
仓库保管员岗位职责
2013/12/20 职场文书
大学理论知识学习自我鉴定
2014/04/28 职场文书
入职担保书怎么写
2014/05/12 职场文书
个人先进事迹总结
2015/02/26 职场文书
创业计划书之儿童理发店
2019/09/27 职场文书
详解Python常用的魔法方法
2021/06/03 Python