python脚本监控websocket服务是否正常

安装websocket-client模块

python环境python3.6+

pip install websocket-client

监控脚本

from websocket import create_connection


def monitor_pulse(url):
    """
    监控websocket服务是否正常
    :param url: websocket连接url
    :return: 返回状态码:101正常;500连接超时
    """
    try:
        ws = create_connection(url, timeout=3)  # 创建连接
        status = ws.getstatus()
        ws.shutdown()  # 关闭连接
    except socket.timeout as e:
        status = 500
    return status

if __name__ == '__main__':
    ws_url = "ws://172.18.21.44:3060"
    print(monitor_pulse(ws_url))

参考:https://www.cnblogs.com/yiyaxuan/p/14685347.html

posted @ 2021-08-12 15:32  chengd  阅读(968)  评论(0)    收藏  举报