正确的 POST 文件上传(使用 Locust 进行负载测试)

Nik*_*eev 10 python django locust

我正在尝试为基于 Django 的网站进行负载测试。

我使用 Locust 0.7.3 和 python 2.7.10

在这里我进行 POST - 填写表格并附上一些文件:

class WebsiteTasks(TaskSet):
    def on_start(self):
        self.client.get("/")

    @task
    def submit(self):
        response = self.client.get("/submit/")
        csrftoken = response.cookies['csrftoken']
        attach = open('file.pdf', 'rb')

        r = self.client.post("/submit/", {
           'csrfmiddlewaretoken': csrftoken,
           'password': smart_str(u'wkefjgui'),
           'payload': smart_str(u'kjsdgfljdsh'),
           'docfile': attach,
           'commit': smart_str(u'???????? / Embed'),
        })
Run Code Online (Sandbox Code Playgroud)

一切似乎都没问题,但是在服务器的上传文件夹中没有文件!

我做错了什么?

Nik*_*eev 8

好吧,我找到了解决方案,我希望它对某人有用:

这里描述了 Django 如何处理文件: How to send a "multipart/form-data" with requests in python?

配方是在 post 函数中定义“文件”参数:

    r = self.client.post("/submit/", data={
        'csrfmiddlewaretoken': csrftoken,
        'password': smart_str(u'wkefjgui'),
        'payload': smart_str(u'kjsdgfljdsh'),
        'commit': smart_str(u'???????? / Embed'),
         }, files={'docfile': attach})
Run Code Online (Sandbox Code Playgroud)