Python进行websocket接口测试

我们在做接口测试时,除了常见的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()

若文章对你有帮助,可以点赞或打赏支持我们。发布者:SmallA,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/python%e8%bf%9b%e8%a1%8cwebsocket%e6%8e%a5%e5%8f%a3%e6%b5%8b%e8%af%95/

(0)
SmallA的头像SmallA认证作者
上一篇 2023年 3月 2日 下午2:57
下一篇 2023年 3月 2日 下午10:30

相关推荐

  • Linux 双机热备

    文章目录Rsync概述什么是Rsyncrsync特性传输方式rsync拉复制实战Rsync推复制 Rsync概述 什么是Rsync rsync是一款开源,快速,多功能的可实现增量的…

    2023年 6月 7日
  • PPTP搭建与连接

    文章目录方法一准备工具搭建pptp服务器方法二在windows下建立vpn连接在linux下建立连接 方法一 准备工具 具备公网ip的电脑 系统:centos 环境:docker、…

    2023年 12月 5日
  • curl 命令详解

    curl 是一种命令行工具,作用是发出网络请求,然后获取数据,显示在”标准输出”(stdout)上面。它支持多种协议,下面列举其常用功能。 文章目录一、查看…

    技术分享 2023年 4月 7日
  • 通过PAC文件配置自动代理

    文章目录代理自动配置文件(PAC)文件语法参数描述预定义的函数与环境示例1 对关键域名进行代理示例2 对除本地主机以外的所有连接使用代理示例3 和例2一样,但是对防火墙外的本地服务…

    2023年 2月 26日
  • Docker 存储目录的平滑迁移

    文章目录docker 目录的迁移迁移准备常用运维指令迁移停止docker服务创建新的目录或磁盘目录相关指令磁盘相关指令删除磁盘分区格式化硬盘建立挂载目录挂载硬盘卸载磁盘从旧目录迁移…

    2021年 11月 4日
  • 生成CA证书

    生成服务器CA证书 在配置HTTPS监听时,您可以使用自签名的CA证书,并且使用该CA证书为客户端证书签名。 执行以下命令,在/home目录下新建一个ca文件夹,并在ca文件夹下创…

    2023年 5月 9日
  • 解决Docker镜像仓库不受信任的问题

    文章目录http:server gave HTTP response to HTTPS client常用解决方式非常用解决方式2场景的解决方式docker场景container场景…

    2021年 11月 17日
  • 为wordpress新增ip和port日志

    应网安要求,需要对用户日志进行扩增,对用户日志新增端口和port记录 文章目录操作大功告成也可以追加如下字段 操作 找到页面文件目录,在wp-includes/funcitons.…

    2023年 2月 26日
  • linux – 终端下查看网络实时吞吐量

    文章目录nload安装使用界面操作设置刷新间隔设置单位设置最大缩放比率总结 nload nload 是一个实时监控网络流量和带宽使用情况,展示进出的流量情况,不仅有数值,也可以有动…

    2023年 10月 9日
  • requests.models.Response的常用属性

    背景:做request 请求的时,返回数据为requests.models.Response。我们需要对返回结果做处理 Response响应的常用属性: response.text…

    技术分享 2023年 3月 7日
Translate »