Ale*_*pov 1 php python laravel locust
尝试使用 Locust 登录 Laravel 应用程序。我的代码如下:
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
self.login()
def login(self):
response = self.client.get("/en/login")
csrftoken = response.cookies['XSRF-TOKEN']
session = response.cookies['bitrent_session']
post_data = {'username': "user@user.com", 'password': '1234', "_token": csrftoken }
with self.client.post('/en/login', post_data,
catch_response=True) as response:
print("Code: ", response.status_code)
print("Content: ", response.content
@task()
def index(self):
self.client.get("/")
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 5000
max_wait = 9000
Run Code Online (Sandbox Code Playgroud)
但是我收到错误 - 代码 419 内容 由于不活动,页面已过期。谁能告诉我如何使用 Locust 登录 Laravel 应用程序
找到答案:
原因是我为 _token 字段传递了错误的值。相反,如果您面临相同的任务,您可以尝试以下操作:
import re
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
self.login()
def login(self):
response = self.client.get("/en/login")
print("Content: ", response.content)
csrftoken = re.search('meta name="csrf-token" content="(.+?)"', response.text).group(1)
print("token: ", csrftoken)
post_data = {'username': "user@user.com", 'password': '1234', "_token": csrftoken }
with self.client.post('/en/login', post_data,
catch_response=True) as response:
print("Code: ", response.status_code)
print("Content: ", response.content)
@task()
def index(self):
self.client.get("/")
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 5000
max_wait = 9000
Run Code Online (Sandbox Code Playgroud)
希望将来能对某人有所帮助:)