Flask get csrf_token 用于 Locust 负载测试
Flask get csrf_token for Locust load testing
我想用我的远程服务器对 Locust 进行负载测试,但我不想禁用 csrf 功能,我怎样才能获得 csrf_token 或绕过它
class UserBehavior(TaskSet):
@task(1)
def login(self):
self.client.get("/securities/login")
token = "how to get it"
self.client.post("/securities/login",{"username":"test",
"password":"123",
"csrf_token":token})
result = self.client.get("/securities/login")
token = result.cookies['_csrf_token'] # i think this is the name anyway you could always print result.cookies to find out
至少我认为...我知道这在 djanjo 中有效
使用 Flask 可能需要多做一些工作...请参阅此要点 https://gist.github.com/singingwolfboy/2fca1de64950d5dfed72
如果你使用Flask_WTF,你可以得到这样的csrf token
:
from flask import Flask
from flask_wtf import FlaskForm
app = Flask(__name__)
app.secret_key = 'random string'
class HelloForm(FlaskForm):
# ...
@app.route('/', methods=['GET', 'POST'])
def index():
form = HelloForm()
token = form.csrf_token.data
相反,您也可以自己生成令牌:
from hashlib import md5
def generate_csrf_token():
token = md5(app.secret_key).hexdigest()
return token
我找到了找到令牌的方法
import re
class UserBehavior(TaskSet):
@task(1)
def login(self):
result = self.client.get("/securities/login")
token = re.search(r'[0-9]{10}##[a-z0-9]{40}',result.text).group(0)
print token
除 re(RegularExpression) 之外的另一种方法是使用 PyQuery 从 html 表单元素中获取令牌键。
我想用我的远程服务器对 Locust 进行负载测试,但我不想禁用 csrf 功能,我怎样才能获得 csrf_token 或绕过它
class UserBehavior(TaskSet):
@task(1)
def login(self):
self.client.get("/securities/login")
token = "how to get it"
self.client.post("/securities/login",{"username":"test",
"password":"123",
"csrf_token":token})
result = self.client.get("/securities/login")
token = result.cookies['_csrf_token'] # i think this is the name anyway you could always print result.cookies to find out
至少我认为...我知道这在 djanjo 中有效
使用 Flask 可能需要多做一些工作...请参阅此要点 https://gist.github.com/singingwolfboy/2fca1de64950d5dfed72
如果你使用Flask_WTF,你可以得到这样的csrf token
:
from flask import Flask
from flask_wtf import FlaskForm
app = Flask(__name__)
app.secret_key = 'random string'
class HelloForm(FlaskForm):
# ...
@app.route('/', methods=['GET', 'POST'])
def index():
form = HelloForm()
token = form.csrf_token.data
相反,您也可以自己生成令牌:
from hashlib import md5
def generate_csrf_token():
token = md5(app.secret_key).hexdigest()
return token
我找到了找到令牌的方法
import re
class UserBehavior(TaskSet):
@task(1)
def login(self):
result = self.client.get("/securities/login")
token = re.search(r'[0-9]{10}##[a-z0-9]{40}',result.text).group(0)
print token
除 re(RegularExpression) 之外的另一种方法是使用 PyQuery 从 html 表单元素中获取令牌键。