Warning: Undefined array key "custom_message" in /www/wwwroot/bbs.aaronyang.cc/wp-content/plugins/wpcopyrights/index.php on line 105

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

相关推荐

  • minio及云存储组件的数据迁移、恢复及文件校验

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

    2023年 8月 28日
  • curl 命令详解

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

    技术分享 2023年 4月 7日
  • requests.models.Response的常用属性

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

    技术分享 2023年 3月 7日
  • 模型量化了解一下?

    编者按:随着深度学习的发展,神经网络被广泛应用于各种领域,模型性能的提高同时也引入了巨大的参数量和计算量。模型量化是一种将浮点计算转成低比特定点计算的技术,可以有效的降低模型计算强…

    2023年 4月 26日
  • 4. FRP概念

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

    2023年 3月 2日
  • 1.FRP概述

    一些概述,便于您快速的了解 frp。 文章目录frp 是什么?为什么使用 frp?下一步? frp 是什么? frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UD…

    2023年 3月 2日
  • 生成CA证书

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

    2023年 5月 9日
  • 如何让自己的模糊视频变高清(4K)?

    有些视频是不是看着很模糊,又不忍心丢掉。比如一段回忆,一个瞬间。下面安利一个很好用的软件,可以通过使用AI深度学习能力,将视频像素进行提升。 文章目录演示视频此软件对电脑要求较高,…

    2021年 7月 29日
  • 树莓派安装ansible教程(二)

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

    2021年 9月 14日
  • PPTP搭建与连接

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

    2023年 12月 5日
Translate »