python接口模拟100个用户登录

  实际工作中需要测试模拟大量坐席在线,机器人转人工之后与坐席建立对话,因此在这之前先模拟坐席账号登录,写的脚本如下:

import requests,pymysql,time,calendar

class Apilogin:
    def __init__(self,seat_id, account=None, password=None):
        if account and password:
            self.account = account
            self.password = password
        else:
            self.account = 'username'
            self.password = 'password'
        self.seat_id = seat_id

    def api_login(self):
        url = ""

        payload = {"username": self.account, "password": self.password, "captcha": "0755", "checkKey": "3c6749bc0d9fc3c1"}
        headers = {
            'Content-Type': 'application/json'
        }
        #登录坐席账号
        response = requests.post(url, headers=headers, json=payload)
        result = response.json()

        print(result)
        return result

    def seatchange(self):

        headers = {
            'Content-Type': 'application/json'
        }

        seatchange_url = ""
        seatinit_url = ""

        seatchange_payload = { "enterprise_id": "865335371",
                    "online_status": "ready",
                    "seat_id": self.seat_id,
                    "voice_switch": True
                    }

        seatinit_payload = {
                    "business_id": [
                        "290024c85f7e4dd88cd23599660c5f8a"
                    ],
                    "channel": "web",
                    "enterprise_id": "865335371",
                    "loginTs": calendar.timegm(time.gmtime()),
                    "seat_id": seatid
                    }
        #将坐席状态置为就绪
        seatchange_response = requests.post(url = seatchange_url, headers=headers, json=seatchange_payload)
        seatchange_result = seatchange_response.json()
        print(seatchange_result)

        #将坐席状态同步坐席监控列表
        seatinit_response = requests.post(url = seatinit_url, headers=headers, json=seatinit_payload)
        seatinit_result = seatinit_response.json()
        print(seatinit_result)
        #return seatchange_result


def conndb():
    conn = pymysql.connect(host='', port=3306, user='root', passwd='', db='', charset='utf8')
    cur = conn.cursor()
    cur.execute("SELECT id,username,realname FROM sys_user WHERE enterprise_code = '865335371'")
    result = cur.fetchall()
    cur.close()
    conn.close()
    return result

if __name__ == '__main__':
    username = conndb()
    #100个用户登录
    for i in range(100):
        account = username[i + 6][1]
        password = str(username[i + 6][1]) + str('password')
        seatid = username[i + 6][0]
        login = Apilogin(seatid,account,password)
        login.api_login()
        time.sleep(1)
        login.seatchange()
原文地址:https://www.cnblogs.com/mtfan01/p/15099436.html