我们在做接口测试时,除了常见的http接口,还有一种比较多见,就是socket接口,今天讲解下怎么用Python进行websocket接口测试。
现在大多数用的都是websocket,那我们就先来安装一下websocket的安装包。
pip install websocket-client
安装完之后,我们就开始我们的websocket之旅了。
我们先来看个炒鸡简单的栗子:
import websocket ws = websocket.WebSocket() ws.connect("ws://example.com/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 terminating...") Thread(target=run).start()
on_open方法是用来保持连接的,上面这样的一个例子,就是保持连接的一个过程,每隔一段时间就会来做一件事,他会在30s内一直发送hello。最后停止。
on_close方法:
def on_close(ws): print("### closed ###")
onclose主要就是关闭socket连接的。
如何创建一个websocket应用:
ws = websocket.WebSocketApp("wss://echo.websocket.org")
括号里面就是你要连接的socket的地址,在WebSocketApp这个实例化的方法里面还可以有其他参数,这些参数就是我们刚刚介绍的这些方法。
ws = websocket.WebSocketApp("ws://echo.websocket.org/", on_message=on_message, on_error=on_error, on_close=on_close)
指定了这些参数之后就可以直接进行调用了,例如:
ws.on_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 terminating...") Thread(target=run).start() if __name__ == "__main__": websocket.enableTrace(True) host = "ws://echo.websocket.org/" ws = websocket.WebSocketApp(host, on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open ws.run_forever()
如果想要通信一条短消息,并在完成后立即断开连接,我们可以使用短连接:
from websocket import create_connection ws = create_connection("ws://echo.websocket.org/") print("Sending 'Hello, World'...") ws.send("Hello, World") print("Sent") print("Receiving...") result = ws.recv() print("Received '%s'" % result) ws.close()
关于websocket的介绍就到这儿了。
以上就是用Python进行websocket接口测试的详细内容,更多关于python 接口测试的资料请关注其它相关文章!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]