#用Python进行websocket接口测试,配合locust性能测试,实现长链接压力测试

#⽤Python进⾏websocket接⼝测试,配合locust性能测试,实现长链接压⼒测试
⽤Python进⾏websocket接⼝测试
我们在做接⼝测试时,除了常见的http接⼝,还有⼀种⽐较多见,就是socket接⼝,今天讲解下怎么⽤Python进⾏websocket接⼝测试。webscoket的属性、⽅法和事件
需要准备的测试环境
安装python,版本3.0以上
安装IDE,pycharm等
安装websocket、websocket-client 包
3、websocket接⼝测试前,把需要⽀持的安装包都安装好
pip install websocket-client
pip install websockets
安装完之后,我们就开始我们的websocket之旅了。
先举个简单的例⼦:
import websocket
ws = websocket.WebSocket()
http_proxy_host="proxy_host_name",
http_proxy_port=3128)
这个栗⼦就是创建⼀个websocket连接,这个模块⽀持通过http代理访问websocket。代理服务器允许使⽤connect⽅法连接到websocket端⼝。默认的squid设置是“只允许连接HTTPS端⼝”。
在websocket⾥,我们有常⽤的这⼏个⽅法:
on_message⽅法:
def on_message(ws, message):
print(message)
on_message是⽤来接受消息的,server发送的所有消息都可以⽤on_message这个⽅法来收取。
on_error⽅法:
def on_error(ws, error):
print(error)
祛斑净这个⽅法是⽤来处理错误异常的,如果⼀旦socket的程序出现了通信的问题,就可以被这个⽅法捕捉到。
on_open⽅法:
def on_open(ws):
def run(*args):
for i in range(30):
# send the message, then wait
# so thread doesn't exit and socket
# isn't closed
ws.send("Hello %d"% i)
time.sleep(1)
time.sleep(1)
ws.close()
print("")
Thread(target=run).start()
on_open⽅法是⽤来保持连接的,上⾯这样的⼀个例⼦,就是保持连接的⼀个过程,每隔⼀段时间就会来做⼀件事,他会在30s内⼀直发送hello。最后停⽌。
on_close⽅法:
def on_close(ws):
print("### closed ###")
onclose主要就是关闭socket连接的。
如何创建⼀个websocket应⽤:
ws = websocket.WebSocketApp("wss://")
括号⾥⾯就是你要连接的socket的地址,在WebSocketApp这个实例化的⽅法⾥⾯还可以有其他参数,这些参数就是我们刚刚介绍的这些⽅法。
ws = websocket.WebSocketApp("ws:///",
on_message=on_message,
铁皮枫斗口服液on_error=on_error,
on_close=on_close)
指定了这些参数之后就可以直接进⾏调⽤了,例如:
<_open = on_open
这样就是调⽤了on_open⽅法
如果我们想让我们的socket保持长连接,⼀直连接着,就可以使⽤run_forever⽅法:
ws.run_forever()
综上所述,完整的应该这么写:
import websocket
from threading import Thread
import time
import sys
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
def run(*args):
for i in range(3):
# send the message, then wait
# so thread doesn't exit and socket
# isn't closed
ws.send("Hello %d"% i)
time.sleep(1)
time.sleep(1)
ws.close()
伏秒特性的绘制方法和含义print("")座便盖
Thread(target=run).start()
三通截止阀if __name__ =="__main__":
host ="ws:///"
ws = websocket.WebSocketApp(host,
on_message=on_message,
on_error=on_error,
on_close=on_close)
<_open = on_open
ws.run_forever()
如果想要通信⼀条短消息,并在完成后⽴即断开连接,我们可以使⽤短连接:
from websocket import create_connection
ws = create_connection("ws:///")
print("Sending 'Hello, World'...")
ws.send("Hello, World")
print("Sent")
print("")
result = ws.recv()
print("Received '%s'"% result)
ws.close()
这⾥更新⼀下,对于websocket 发送如果我们直接发送,很可能因为格式不正确,服务端不能解析,这时可以考虑,导⼊json包对 发送的数据进⾏解析,注意把代码放在对应的位置上。
import json
Data={"channel":"xxx","market":'xxx',"event":"xxxx"}
ws.send(json.dumps(Data).encode('utf8'))
json dump编码,loads 解码,或者说反序列化⽐较好,dumps是把python中的数据对象,序列化成json格式的字符串;
loads是把json格式的字符串反序列化为python中的数据对象
obj = json.t.decode(‘utf8’))#据说是终极⽅法,获取内容⾃⼰解码,通常情况下,响应消息体是json格式都需要转换为python中的数据对象
print(obj)排火
print(obj[‘form’])#如果不处理,取数据是很⿇烦的
print(obj[‘form’][‘2’])

本文发布于:2024-09-22 15:37:46,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/224268.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:连接   格式   测试
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议