用Python进行websocket接口测试
作者:我被狗咬了 发布时间:2022-03-02 09:44:22
我们在做接口测试时,除了常见的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的介绍就到这儿了。
来源:https://cloud.tencent.com/developer/article/1510529


猜你喜欢
- 背景最近尝试了解Django中ORM实现的原理,发现其用到了metaclass(元类)这一技术,进一步又涉及到Python class中有两
- 自动化测试的脚本运行完成之后,可以生成test report,如果能将result自动的发到邮箱就不用每次打开阅读,而且随着脚本的不段运行,
- 目录安装基本操作打开图像转换格式展示图片剪裁合并缩略图旋转滤镜二次创作画线文字总结文 | 豆豆来源:Python 技术「ID: python
- 案例故事:大部分带彩色屏幕的终端设备,不管是手机,车机,电视等等,都需要涉及图片的显示,作为一名专业的多媒体测试人员,我们需要一堆的规范化标
- 前言如果你在寻找python工作,那你的面试可能会涉及Python相关的问题。通过对网络资料的收集整理,本文列出了100道python的面试
- 登陆流程图:代码实现:#-*- coding=utf-8 -*-import os,sys,getpass'''us
- 根据 homebrew-brew 官方的解释得知,MongoDB 不再是开源的了,并且已经从 Homebrew中移除 #43770正是由于
- 本篇博文主要讲解Python爬虫实例,重点包括爬虫技术架构,组成爬虫的关键模块:URL管理器、HTML下载器和HTML解析器。爬虫简单架构程
- MYSQLdump参数详解mysqldump备份:mysqldump -u用户名 -p密码 -h主机 数据库 a -w “sql条件” –l
- 本例以Linux上安装Pyhton3.8版本为例进行说明1、依赖包安装yum -y install zlib-devel bzip2-dev
- 深入理解python try异常处理机制#python的try语句有两种风格#一:种是处理异常(try/except/else)#二:种是无
- 1.加载数据库,数据库的配置不能写死在seting.py文件中,下面的方式是读取另外一个文件,配置数据库:config = '
- Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的
- Python是一种计算机程序设计语言,一种面向对象的动态类型语言,一种脚本语言。最初被设计用于编写自动化脚本(shell)的,常用于各种服务
- 实现效果如下: 需求:由于后台搜索选项有很多,影响页面美观,所以一进来要隐藏一部分搜索项,只保留1行,点击【展开搜索】按钮的时候才
- 本文实例讲述了python自定义类并使用的方法。分享给大家供大家参考。具体如下:class Person: def __init
- 客户端: <%@ Page Language="C#" AutoEventWireup="true&qu
- oracle数据库的快照是一个表,它包含有对一个本地或远程数据库上一个或多个表或视图的查询的结果。正因为快照是一个主表的查询子集,使用快照可
- 例如torch.nn.ReLU(inplace=True)inplace=True表示进行原地操作,对上一层传递下来的tensor直接进行修
- ‘Microsoft OLE DB Provider for ODBC Drivers (0x80004005) [Microsoft][O