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

相关推荐

  • 阿里云Raw镜像文件如何直接打开

    文章目录前言1.下载OSS中备份好的镜像文件2.将raw文件进行解压3.挂载raw格式镜像方法4.卸载raw格式镜像 前言 由于之前做服务器迁移,所以把云主机上的数据和镜像都做了备…

    2023年 2月 26日
  • 为wordpress新增ip和port日志

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

    2023年 2月 26日
  • curl 命令详解

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

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

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

    2021年 9月 14日
  • Apsara Clouder云计算专项技能认证:云服务器ECS入门[考试真题分享]

    1.下列哪一个不是重置ECS密码的步骤? A.查看实例详情 B.进入控制台 C.远程连接ECS D.点击控制台“概览” 2.云服务器ECS以服务化的方式对客户提供,阿里云产品售后支…

    技术分享 2023年 2月 4日
  • 2.1 使用 systemd

    这个示例将会演示在 Linux 系统下使用 systemd 控制 frps 及配置开机自启。 在 Linux 系统下,使用systemd 可以方便地控制 frp 服务端 frps …

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

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

    2021年 11月 17日
  • 4. FRP概念

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

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

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

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

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

    2023年 4月 11日
Translate »