用Python进行websocket接口测试


Posted in Python onOctober 16, 2020

我们在做接口测试时,除了常见的http接口,还有一种比较多见,就是socket接口,今天讲解下怎么用Python进行websocket接口测试。

现在大多数用的都是websocket,那我们就先来安装一下websocket的安装包。

pip install websocket-client

用Python进行websocket接口测试

安装完之后,我们就开始我们的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 接口测试的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
使用IronPython把Python脚本集成到.NET程序中的教程
Mar 31 Python
详解Python的Lambda函数与排序
Oct 25 Python
python学习基础之循环import及import过程
Apr 22 Python
对Python3之进程池与回调函数的实例详解
Jan 22 Python
Python可视化mhd格式和raw格式的医学图像并保存的方法
Jan 24 Python
10招!看骨灰级Pythoner玩转Python的方法
Apr 15 Python
Python空间数据处理之GDAL读写遥感图像
Aug 01 Python
pandas使用之宽表变窄表的实现
Apr 12 Python
PyQt5 QDockWidget控件应用详解
Aug 12 Python
Python使用requests模块爬取百度翻译
Aug 25 Python
Pytorch DataLoader shuffle验证方式
Jun 02 Python
python中opencv实现图片文本倾斜校正
Jun 11 Python
python如何控制进程或者线程的个数
Oct 16 #Python
python利用 keyboard 库记录键盘事件
Oct 16 #Python
python实现快速文件格式批量转换的方法
Oct 16 #Python
Python通过getattr函数获取对象的属性值
Oct 16 #Python
pandas处理csv文件的方法步骤
Oct 16 #Python
python爬取”顶点小说网“《纯阳剑尊》的示例代码
Oct 16 #Python
Python使用内置函数setattr设置对象的属性值
Oct 16 #Python
You might like
使用bcompiler对PHP文件进行加密的代码
2010/08/29 PHP
一个基于phpQuery的php通用采集类分享
2014/04/09 PHP
CI使用Tank Auth转移数据库导致密码用户错误的解决办法
2014/06/12 PHP
PHP IDE PHPStorm配置支持友好Laravel代码提示方法
2015/05/12 PHP
php中array_fill函数的实例用法
2021/03/02 PHP
输入框的字数时时统计—关于 onpropertychange 和 oninput 使用
2011/10/21 Javascript
javascript 系统文件夹文件操作及参数介绍
2013/01/08 Javascript
php is_numberic函数造成的SQL注入漏洞
2014/03/10 Javascript
JavaScript使用cookie实现记住账号密码功能
2015/04/27 Javascript
javascript截取字符串小结
2015/04/28 Javascript
JavaScript模块规范之AMD规范和CMD规范
2015/10/27 Javascript
javascript html5 canvas实现可拖动省份的中国地图
2016/03/11 Javascript
基于jQuery实现Ajax验证用户名是否存在实例
2016/03/30 Javascript
使用vue和datatables进行表格的服务器端分页实例代码
2017/06/07 Javascript
JS 设置Cookie 有效期 检测cookie
2017/06/15 Javascript
nodejs socket服务端和客户端简单通信功能
2017/09/14 NodeJs
原生JS实现$.param() 函数的方法
2018/08/10 Javascript
vue-cli中安装方法(图文详细步骤)
2018/12/12 Javascript
[04:03]DOTA2英雄梦之声_第02期_风暴之灵
2014/06/30 DOTA
Python Requests模拟登录实现图书馆座位自动预约
2018/04/27 Python
使用python实现男神女神颜值打分系统(推荐)
2019/10/31 Python
Python求凸包及多边形面积教程
2020/04/12 Python
Python实现Word表格转成Excel表格的示例代码
2020/04/16 Python
TensorFlow中如何确定张量的形状实例
2020/06/23 Python
巴黎卡诗美国官方网站:始于1964年的头发头皮护理专家
2017/07/10 全球购物
Lulu & Georgia官方网站:购买地毯、家具、抱枕、壁纸、床上用品等
2018/03/19 全球购物
斯洛伐克香水和化妆品购物网站:Parfemy-Elnino.sk
2020/01/28 全球购物
应聘收银员个人的求职信
2013/11/30 职场文书
房地产财务部员工岗位职责
2014/03/12 职场文书
爱之链教学反思
2014/04/30 职场文书
护士感人事迹
2014/05/01 职场文书
高职教师先进事迹材料
2014/08/24 职场文书
2014年妇联工作总结
2014/11/21 职场文书
端午节活动总结报告
2015/02/11 职场文书
Python机器学习之基于Pytorch实现猫狗分类
2021/06/08 Python
alibaba seata服务端具体实现
2022/02/24 Java/Android