看注释
# 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)