运行Locust时为什么会出现403错误?

问题描述 投票:9回答:2

我正在使用Locust(python)在Django Web应用程序上加载测试。我运行脚本时遇到403错误。

这是代码:

  from locust import HttpLocust, TaskSet

def index(l):
    l.client.get("/")
def login(l):
    l.client.post("/login/", {"username":"[email protected]", "password":"education")
def upload(l):
    l.client.get("/upload-image/")
def home(l):
	 l.client.get("/home/")
def settings(l):
	l.client.get("/settings/")
def logout(l):
	l.client.get("/logout/")
class UserBehavior(TaskSet):
    tasks = {index:1, upload:1, home:1, settings:1, logout:1}

    def on_start(self):
        login(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait=5000
    max_wait=9000
python django load-testing http-status-code-403 locust
2个回答
11
投票

要扩展ZacDelagrange的答案,当您使用https时,您还必须设置Referer标题,因此在此示例中您可以执行

def on_start(self):
    """ Run on start for every Locust hatched """
    r = self.client.get('')
    self.client.headers['Referer'] = self.client.base_url
    self.client.post('/accounts/login/', 
        {'email': 'email', 'password': 'password',
         'csrfmiddlewaretoken': r.cookies['csrftoken']})

8
投票

进入根目录或登录页面,从响应cookie中获取csrf令牌,然后使用csrftoken发布到您的登录URL。这应该将csrf令牌添加到客户端的cookie中,并允许您浏览该页面。

def on_start(self):
    """ Run on start for every Locust hatched """
    r = self.client.get('')
    self.client.post('/accounts/login/', 
        {'email': 'email', 'password': 'password',
         'csrfmiddlewaretoken': r.cookies['csrftoken']})
© www.soinside.com 2019 - 2024. All rights reserved.