尝试按照文档使用 Locust, https://docs.locust.io/en/stable/quickstart.html
from locust import HttpUser, task
class TestUser(HttpUser):
# @task(1)
# def health(self):
# self.client.get("/health")
@task(2)
def post_value(self):
self.client.post(
"/some/route",
headers={
"key1": "value1"
},
data={
"data1": "value1",
})
GET 请求工作正常。 POST 请求在 locust client.py 中引发以下错误。
File "/opt/anaconda3/envs/email_bot/lib/python3.12/site-packages/locust/clients.py", line 286, in post
return self.request("POST", url, data=data, json=json, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/anaconda3/envs/email_bot/lib/python3.12/site-packages/locust/clients.py", line 196, in request
url = request_before_redirect.url # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'url'
尝试设置
allow_redirects=True
但仍然出现相同的错误。无法识别根本问题。任何指示都会有帮助。
我在我的环境上检查了一下,理论上没有错误。代码有效
from locust import HttpUser, task
class TestUser(HttpUser):
@task(2)
def post_value(self):
user = 'test'
self.client.post(
'/platform/j_spring_security_check',
headers={
'User-Agent': 'Test 66',
},
data={
'j_username': user,
'j_password': user,
'j_rememberme': 'false'
}
)
Nginx 日志,您可以在其中看到我收到的响应
status code 200
并且考虑了我在 User-Agent
中传递的 headers
:11:52:06 +0000] "POST /platform/j_spring_security_check HTTP/1.1" 200 0 "-" "Test 66"