Python 蝗虫导出请求和响应主体到csv文件

Python 蝗虫导出请求和响应主体到csv文件,python,locust,Python,Locust,我正在从.csv文件中读取数据以发送api请求。 我想将失败的请求和响应主体导出到.csv或.xlsx文件中 这段代码运行良好,但正如我所说,我需要请求和响应主体。 我在@events.request\u failure.add\u侦听器中找不到true关键字 我应该为此编写新函数吗?是否将r.json()转换为csv文件 这是我的密码: stat_file = open('stats.csv', 'w') class MyUser(HttpUser): @task(1) de

我正在从.csv文件中读取数据以发送api请求。 我想将失败的请求和响应主体导出到.csv或.xlsx文件中

这段代码运行良好,但正如我所说,我需要请求和响应主体。 我在@events.request\u failure.add\u侦听器中找不到true关键字

我应该为此编写新函数吗?是否将r.json()转换为csv文件

这是我的密码:

stat_file = open('stats.csv', 'w')
class MyUser(HttpUser):

    @task(1)
    def langdetect(self):
        customer = next(ssn_reader)
        r = self.client.post("/api/v1/customer", data=json.dumps({
            "text": f"/?ssn={customer[0]}"
        }),
                         headers=headers,
                         )
        print(r.text)        


# hook that is fired each time the request ends up with failure
@events.request_failure.add_listener
def hook_request_failure(request_type, name, response_time, response_length, **kw):
    stat_file.write(request_type + ";" + name + ";" + str(response_time) + ";" + str(response_length) + "\n")



@events.quitting.add_listener
def hook_quitting(environment, **kw):
    stat_file.close()

蝗虫1.5中的新功能解决了您的问题吗?您现在可以添加一个自由形式的“上下文”,并在请求处理程序中访问响应