Why am I getting 403 error when starting Locust?

I am using Locust (python) to load a test in a Django web application. I keep getting 403 error when I run my script.

Here is the code:

from locust import HttpLocust, TaskSet def index(l): l.client.get("/") def login(l): l.client.post("/login/", {"username":" an@id.com ", "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 
+9
source share
2 answers

To extend the ZacDelagrange answer when you use https, you should also set the Referer header, so in this example you can do

 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']}) 
+12
source

Log in to your root or login page, grab the csrf token from the response cookie, and publish your login URL using csrftoken. This should add the csrf token to the client's cookies and allow you to view the page.

 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']}) 
+7
source

Source: https://habr.com/ru/post/1208198/


All Articles