用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 相关文章推荐
python字符串加密解密的三种方法分享(base64 win32com)
Jan 19 Python
python打开url并按指定块读取网页内容的方法
Apr 29 Python
Python使用os模块和fileinput模块来操作文件目录
Jan 19 Python
Python安装使用命令行交互模块pexpect的基础教程
May 12 Python
详解Python在七牛云平台的应用(一)
Dec 05 Python
Python数字图像处理之霍夫线变换实现详解
Jan 12 Python
Python中sort和sorted函数代码解析
Jan 25 Python
python3 pillow模块实现简单验证码
Oct 31 Python
JupyterNotebook设置Python环境的方法步骤
Dec 03 Python
python 命名规范知识点汇总
Feb 14 Python
Python使用qrcode二维码库生成二维码方法详解
Feb 17 Python
JAVA SpringMVC实现自定义拦截器
Mar 16 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
php 编写安全的代码时容易犯的错误小结
2010/05/20 PHP
php读取富文本的时p标签会出现红线是怎么回事
2014/05/13 PHP
PHP访问Google Search API的方法
2015/03/05 PHP
PHP封装的HttpClient类用法实例
2015/06/17 PHP
php版微信自动获取收货地址api用法示例
2016/09/22 PHP
总结的一些PHP开发中的tips(必看篇)
2017/03/24 PHP
Javascript学习笔记二 之 变量
2010/12/15 Javascript
THREE.JS入门教程(1)THREE.JS使用前了解
2013/01/24 Javascript
jquery选择器、属性设置用法经验总结
2013/09/08 Javascript
jqGrid增加时--判断开始日期与结束日期(实例解析)
2013/11/08 Javascript
Angularjs基础知识及示例汇总
2015/01/22 Javascript
基于JS快速实现导航下拉菜单动画效果附源码下载
2016/10/27 Javascript
jQuery源码分析之sizzle选择器详解
2017/02/13 Javascript
Bootstrap媒体对象学习使用
2017/03/07 Javascript
浅谈redux以及react-redux简单实现
2018/08/28 Javascript
详解Vue的常用指令v-if, v-for, v-show,v-else, v-bind, v-on
2018/10/12 Javascript
[06:33]3.19 DOTA2发布会 海涛、冷冷、2009见证希望
2014/03/21 DOTA
python中使用enumerate函数遍历元素实例
2014/06/16 Python
Python wxPython库使用wx.ListBox创建列表框示例
2018/09/03 Python
python一键去抖音视频水印工具
2018/09/14 Python
解决Python中pandas读取*.csv文件出现编码问题
2019/07/12 Python
利用Python实现朋友圈中的九宫格图片效果
2020/09/03 Python
Python txt文件如何转换成字典
2020/11/03 Python
在Pycharm中安装Pandas库方法(简单易懂)
2021/02/20 Python
CSS3中border-radius属性设定圆角的使用技巧
2016/05/10 HTML / CSS
5个你不知道的HTML5的接口介绍
2013/08/07 HTML / CSS
html5使用canvas画一条线
2014/12/15 HTML / CSS
阿里巴巴国际站:Alibaba.com
2016/07/21 全球购物
Foot Locker澳洲官网:美国运动服和鞋类零售商
2019/10/11 全球购物
教育实习生的自我评价分享
2013/11/21 职场文书
高二历史教学反思
2014/01/25 职场文书
淘宝店铺营销方案
2014/02/13 职场文书
大学生村官考核材料
2014/05/23 职场文书
我在伊朗长大观后感
2015/06/16 职场文书
采购部2015年度工作总结
2015/07/24 职场文书
详解gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)
2021/11/27 Vue.js