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

相关推荐

  • 3. FRP概念

    一些概念,理解它们有助于您更好地了解和使用 frp。 文章目录原理代理代理类型 原理 frp 主要由 客户端(frpc) 和 服务端(frps) 组成,服务端通常部署在具有公网 I…

    2023年 3月 2日
  • 内网环境搭建NTP服务器

    文章目录说在前面:ntp和ntpdate区别简介:ntp全名 network time protocol 。NTP服务器可以为其他主机提供时间校对服务。环境准备:两台服务器,一台作…

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

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

    2023年 5月 9日
  • 4. FRP概念

    文章目录通过 SSH 访问内网机器通过自定义域名访问内网的 Web 服务转发 DNS 查询请求转发 Unix 域套接字对外提供简单的文件访问服务为本地 HTTP 服务启用 HTTP…

    2023年 3月 2日
  • minio及云存储组件的数据迁移、恢复及文件校验

    文章目录0.介绍1.下载备份工具2.解压备份工具3.配置4.测试配置是否正确5.备份5.1 备份至本地文件夹5.2 从minioA 到 minioB6. 数据校验6.1 数据容量校…

    2023年 8月 28日
  • 2.FRP安装

    文章目录安装系统需求下载部署开始使用!使用 systemd 安装 关于如何安装 frp 的说明。 frp 采用 Golang 编写,支持跨平台,仅需下载对应平台的二进制文件即可执行…

    2023年 3月 2日
  • Linux 定时执行shell 脚本

    crontab 可以在指定的时间执行一个shell脚本以及执行一系列 Linux 命令。 文章目录定时执行shellcrontab命令详解常用命令基本格式示例错误捕获 定时执行sh…

    技术分享 2023年 12月 5日
  • 树莓派安装ansible教程(二)

    ansible是一款方便大家对集群计算机进行管理的软件,此软件通过对master节点的设置,通过配置对slave节点的免密服务,完成部署后,即可在master操作一条指令,同时对多…

    2021年 9月 14日
  • 通过PAC文件配置自动代理

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

    2023年 2月 26日
  • linux主机与主机之间的免密登陆配置

    文章目录秘钥分发生成密钥对依次拷贝密钥对其他主机需要执行的指令对本机需要执行的指令尝试登陆本地主机,观察是否为免密登陆 秘钥分发 生成密钥对 ssh-keygen -t rsa 依…

    2023年 1月 29日
Translate »