如何用Python编写脚本测试WebSocket接口的稳定性?
- 内容介绍
- 文章标签
- 相关推荐
本文共计444个文字,预计阅读时间需要2分钟。
在进行接口测试时,除了常见的HTTP接口,还有一种比较少见但使用较多的接口类型,那就是socket接口。今天我将讲解如何使用Python进行websocket接口测试。现在,大多数应用都使用websocket,因此我们首先来安装一下webs库。
我们在做接口测试时,除了常见的example.com/websocket", 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 接口测试的资料请关注易盾网络其它相关文章!
本文共计444个文字,预计阅读时间需要2分钟。
在进行接口测试时,除了常见的HTTP接口,还有一种比较少见但使用较多的接口类型,那就是socket接口。今天我将讲解如何使用Python进行websocket接口测试。现在,大多数应用都使用websocket,因此我们首先来安装一下webs库。
我们在做接口测试时,除了常见的example.com/websocket", 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 接口测试的资料请关注易盾网络其它相关文章!

