用Python进行websocket接口测试

我们在做接口测试时,除了常见的http接口,还有一种比较多见,就是socket接口,今天讲解下怎么用Python进行websocket接口测试。

现在大多数用的都是websocket,那我们就先来安装一下websocket的安装包。

1

pip install websocket-client

 

安装完之后,我们就开始我们的websocket之旅了。

我们先来看个炒鸡简单的栗子:

1

2

3

4

5

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方法:

1

2

def on_message(ws, message):

  print(message)

on_message是用来接受消息的,server发送的所有消息都可以用on_message这个方法来收取。

on_error方法:

1

2

def on_error(ws, error):

  print(error)

这个方法是用来处理错误异常的,如果一旦socket的程序出现了通信的问题,就可以被这个方法捕捉到。

on_open方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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方法:

1

2

def on_close(ws):

  print("### closed ###")

onclose主要就是关闭socket连接的。

如何创建一个websocket应用:

1

ws = websocket.WebSocketApp("wss://echo.websocket.org")

括号里面就是你要连接的socket的地址,在WebSocketApp这个实例化的方法里面还可以有其他参数,这些参数就是我们刚刚介绍的这些方法。

1

2

3

4

ws = websocket.WebSocketApp("ws://echo.websocket.org/",

              on_message=on_message,

              on_error=on_error,

              on_close=on_close)

指定了这些参数之后就可以直接进行调用了,例如:

1

ws.on_open = on_open

这样就是调用了on_open方法

如果我们想让我们的socket保持长连接,一直连接着,就可以使用run_forever方法:

1

ws.run_forever()

完整代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

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()

如果想要通信一条短消息,并在完成后立即断开连接,我们可以使用短连接:

1

2

3

4

5

6

7

8

9

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()

​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

下面是一个使用Python测试WebSocket接口的示例代码: ```python import websocket import json # 定义WebSocket接收到消息时的处理函数 def on_message(ws, message): print("Received message: " + message) # 定义WebSocket连接建立时的处理函数 def on_open(ws): print("WebSocket connection established") # 向WebSocket服务器发送消息 message = {"type": "test", "data": "hello"} ws.send(json.dumps(message)) # 定义WebSocket连接关闭时的处理函数 def on_close(ws): print("WebSocket connection closed") # 定义WebSocket连接错误时的处理函数 def on_error(ws, error): print("WebSocket error: " + str(error)) # 创建WebSocket连接 ws = websocket.WebSocketApp("ws://localhost:8080/ws", on_message = on_message, on_open = on_open, on_close = on_close, on_error = on_error) # 开始WebSocket连接 ws.run_forever() ``` 在上面的代码中,我们使用了Python的`websocket`库,定义了四个回调函数: - `on_message`: 当WebSocket接收到消息时调用的函数。 - `on_open`: 当WebSocket连接建立时调用的函数。 - `on_close`: 当WebSocket连接关闭时调用的函数。 - `on_error`: 当WebSocket连接出现错误时调用的函数。 然后我们创建了一个WebSocket连接对象,指定了上面定义的四个回调函数,并调用其`run_forever`方法开始连接。在`on_open`函数中,我们向WebSocket服务器发送了一条消息。 请注意,上面的代码仅供参考,具体的WebSocket接口测试代码需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值