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

yipeiwu_com4年前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设计】。

相关文章

详解python3中tkinter知识点

#导入tkinter模块,以及导入ttk模块,tkinter是python结合tk的标准接口,ttk是TK8.5之后加入的“主题化工具包” from tkinter import *...

Python 绘图库 Matplotlib 入门教程

Python 绘图库 Matplotlib 入门教程

运行环境 由于这是一个Python语言的软件包,因此需要你的机器上首先安装好Python语言的环境。关于这一点,请自行在网络上搜索获取方法。 关于如何安装Matplotlib请参见这里:...

Django 使用easy_thumbnails压缩上传的图片方法

easy_thumbnails:A powerful, yet easy to implement thumbnailing application for Django 1.4+ 安...

pytorch实现用Resnet提取特征并保存为txt文件的方法

接触pytorch一天,发现pytorch上手的确比TensorFlow更快。可以更方便地实现用预训练的网络提特征。 以下是提取一张jpg图像的特征的程序: # -*- coding...

pycharm配置git(图文教程)

pycharm配置git(图文教程)

下载git客户端  FileàDefault Settingà Version Controlà Git Path to Git executable 填写git客户端的git...