hyload中解决性能测试时的图片验证码

首先在黑羽压测hyload中安装ddddocr库

import ddddocr
client = HttpClient('127.0.0.1:80', # 目标地址:端口
                timeout=10   # 超时时间,单位秒
               ) 
ocr = ddddocr.DdddOcr()
#请求验证码
response = client.sendAndRecv(
'GET',
  'http://xxxxxxx/verifyCode.action?timeStamp16765xxxxxxx',
  headers={
    'Accept':'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate',
    'Accept-Language':'zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6',
    'Cache-Control':'no-cache',
    'Connection':'keep-alive',
    'Cookie':'JSESSIONID=6846C8A151F5B2E3xxxxxxxxxxxx6E60FE6',
    'DNT':'1',
    'Host':'xxxxxxx',
    'Pragma':'no-cache',
    'Referer':'http://xxxxxxx/index/login?redirect=%2F',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
  },

)

#保存验证码为本地图片
with open("verifyCode.jpg",'wb') as fw:
    fw.write(response.raw)
#识别验证码
with open("verifyCode.jpg",'rb') as f:
    image = f.read()
res = ocr.classification(image)#得到验证码字符串

# 提交登录请求
response = client.sendAndRecv(
'POST',
  'http://sssssss',
  headers={
    'Accept':'application/json, text/plain, */*',
    'Accept-Encoding':'gzip, deflate',
    'Accept-Language':'zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6',
    'Cache-Control':'no-cache',
    'Connection':'keep-alive',
    'Content-Length':'95',
    'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
    'Cookie':'JSESSIONID=6846C8A151F5B2E337CD741E66E60FE6',
    'DNT':'1',
    'Host':'sssssss',
    'Origin':'http://sssssss',
    'Pragma':'no-cache',
    'Referer':'http://sssssss/index/login?redirect=%2F',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
  },

  data=r'''account=testn&username=test&Password=123456&verifyCode=%s''' % res
)

更多用法见 https://github.com/sml2h3/ddddocr

多说一嘴。其实大多数时候不建议在性能测试时从客户端处理验证码,会增加负载机的负担。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值