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

yipeiwu_com5年前Python基础

本文实例为大家分享了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设计】。

相关文章

Pytorch之卷积层的使用详解

1.简介(torch.nn下的) 卷积层主要使用的有3类,用于处理不同维度的数据 参数 Parameters: in_channels(int) – 输入信号的通道 out_channe...

Pytorch训练过程出现nan的解决方式

Pytorch训练过程出现nan的解决方式

今天使用shuffleNetV2+,使用自己的数据集,遇到了loss是nan的情况,而且top1精确率出现断崖式上升,这显示是不正常的。 在网上查了下解决方案。我的问题是出在学习率上了...

Python生成密码库功能示例

本文实例讲述了Python生成密码库功能。分享给大家供大家参考,具体如下: 这个代码是将字符的所有组合添加到一个文件中,可以设置密码的最大长度,我这里设置的是8位,但是要有心里准备,生成...

python使用matplotlib库生成随机漫步图

python使用matplotlib库生成随机漫步图

本教程使用python来生成随机漫步数据,再使用matplotlib将数据呈现出来 开发环境 操作系统: Windows10 IDE: Pycharm 2017.1.3 Python...

python开发之for循环操作实例详解

本文实例讲述了python开发之for循环操作。分享给大家供大家参考,具体如下: 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [2,"Jon...