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

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

相关文章

pygame实现成语填空游戏

pygame实现成语填空游戏

最近看到很多人玩成语填字游戏,那么先用pygame来做一个吧,花了大半天终于完成了,附下效果图。 偷了下懒程序没有拆分,所有程序写在一个文件里,主要代码如下: # -*- codi...

Python中装饰器高级用法详解

在Python中,装饰器一般用来修饰函数,实现公共功能,达到代码复用的目的。在函数定义前加上@xxxx,然后函数就注入了某些行为,很神奇!然而,这只是语法糖而已。 场景 假设,有一些工作...

python utc datetime转换为时间戳的方法

最近python代码遇到了一个神奇的需求, 就是如果将python utc datetime转换为时间戳. 百度找到都是使用time.mktime(xxx) 但是看到官网文档里写 t...

使用Python自动化破解自定义字体混淆信息的方法实例

注意:本示例仅供学习参考~ 混淆原理 出于某种原因,明文信息通过自定义字体进行渲染,达到混淆目的。 举个例子: 网页源码 <p>123</p> 在正常字体的渲染下...

python中copy()与deepcopy()的区别小结

python中copy()与deepcopy()的区别小结

前言 copy()与deepcopy()之间的区分必须要涉及到python对于数据的存储方式。 深复制被复制对象完全再复制一遍作为独立的新个体单独存在。所以改变原有被复制对象不会对已经复...