python脚本-随机抽取user发送websocket群聊消息,固定消息顺序(便于check接收)

看注释

# coding=utf-8
import random, json, requests, time
from websocket import create_connection

# 初始化user列表,目前test账号13711110001至13711110050
user_list = []
for a in range(1, 51):
    a = str(a).zfill(4)     # 4位长度,前面补0
    userPhone = '1371111%s'%(a)
    user_list.append(userPhone)
print(user_list)

class test():
    def user_login(self,userPhone,userPasswd):
        url = "https://xxx.com/ywwl-admin/user/login"
        headers = {"Content-Type":"application/json"}
        body = {"userPhone": userPhone, "userPasswd": userPasswd, "projectId": "PROAUTH"}
        user_json = requests.post(url, data = json.dumps(body), headers=headers)
        user_token = user_json.json()['data']['userToken']
        return user_token

    def im_socket(self,user_token,user_num):
        try:
            ws = create_connection("wss://xxx.com/websocket?platform=1&token=%s&bizType=1"%(user_token))     
            print(f'websocket连接状态:{ws.connected};当前使用token为{user_token}')
            if ws.connected == True:
                message = '{"event":1,"data":{"msgType":1,"sessionId":"1684491776793976832","msgContent":"test消息%s"}}'%(user_num)   
                print(message)
                ws.send(message)
                # print(f'接收返回结果:{ws.recv()}')
            else:
                print(f'websocket连接失败!!!!!!!!!!!!!!!')
            ws.close()
        except Exception as e:
            return e

wh = test()
print('============开始执行登录,获取用户token============')
token_list = []
for i in user_list:
    user_token = wh.user_login(i, "111111")
    token_list.append(user_token)
print(token_list)

print('============多用户循环发送消息============')
top_num = int(time.time())
for user_num in range(1, 21):
    wh.im_socket(random.choice(token_list), f'{top_num}序号{user_num}')  # 从token_list随机提取一个token去发送消息
    # time.sleep(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值