没有为 Locust 客户端创建 Django sessionid

Django sessionid not created for Locust client

我运行正在使用 Django 2.2.24 和 Locust 2.1.0 进行负载测试。当我 运行 一些 Locust 测试以 Django 管理员用户身份登录时,没有任何问题 - 我的所有 GET 都按预期工作。

现在我实际上是以特定用户身份登录的。从 Web 界面,Django 毫无问题地传递了一个 CSRFtoken 和一个 sessionid 令牌。然而,在 Locust 客户端中,sessionid 根本不显示。不仅如此,当我查看 django_session table(Web 令牌确实存在的地方)时,没有用于 Locust 客户端的令牌。

我认为这与 Django 会话管理的关系比与 Locust 的关系更密切 - 因此本论坛中的 post。

我的 locust 文件如下所示:

def on_start(self):
    '''
    The start script for each user - this order is important
    '''
    # Below 3 lines work fine - we get the csrftoken and put it in the header successfully
    response = self.client.get("/accounts/login")
    self.csrftoken = response.cookies['csrftoken']
    self.headers = {'X-CSRFToken': self.csrftoken}

    # Now login with username and password as POST
    r1 = self.login()
    return r1

def login(self):
    # admin login  and retrieving it's access token
    
    udata = {'username': self.username, 'password': self.password}
    cookies=self.client.cookies.get_dict())
    #csrftoken cookie does exist, sessionid does not yet.
    log.info("Current cookies in Login:" + str(self.client.cookies))
    # This next line should come back with a sessionid 
    # from Django - but it does not.
    response = self.client.post("/accounts/login/",
                                data=json.dumps(udata),
                                headers=self.headers)
    log.info("Response from client.post="+str(response)) #OK
    log.info("Response status code:" + str(response.status_code))
    log.info("Response text=" + response.text)
    # Next line does not contain sessionid or Set-Cookie 
    log.info("Headers from post/accts/login = " + str(response.headers)) 

感谢您的帮助。

改变

response = self.client.post("/accounts/login/",
                            data=json.dumps(udata),
                            headers=self.headers)

response = self.client.post("/accounts/login/",
                            json=udata,
                            headers=self.headers) 

…得到合适的jsonheaders。或者使用:

response = self.client.post("/accounts/login/",
                            data=udata,
                            headers=self.headers)

以便将其作为 form-encoded 数据而不是 json 发送。