45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
|
|
import requests
|
||
|
|
from bs4 import BeautifulSoup
|
||
|
|
|
||
|
|
# 创建会话
|
||
|
|
session = requests.Session()
|
||
|
|
|
||
|
|
# 首先获取登录页面以获取CSRF令牌
|
||
|
|
print("获取登录页面...")
|
||
|
|
login_page = session.get('http://localhost:5000/login')
|
||
|
|
print(f"登录页面状态: {login_page.status_code}")
|
||
|
|
|
||
|
|
# 解析CSRF令牌
|
||
|
|
soup = BeautifulSoup(login_page.text, 'html.parser')
|
||
|
|
csrf_token_input = soup.find('input', {'name': 'csrf_token'})
|
||
|
|
csrf_token = csrf_token_input['value'] if csrf_token_input else ''
|
||
|
|
|
||
|
|
print(f"CSRF Token: {csrf_token}")
|
||
|
|
|
||
|
|
# 测试登录
|
||
|
|
login_data = {
|
||
|
|
'username': 'admin',
|
||
|
|
'password': 'admin123',
|
||
|
|
'csrf_token': csrf_token
|
||
|
|
}
|
||
|
|
|
||
|
|
print("测试登录...")
|
||
|
|
response = session.post('http://localhost:5000/login', data=login_data)
|
||
|
|
print(f"登录状态: {response.status_code}")
|
||
|
|
|
||
|
|
# 检查是否登录成功
|
||
|
|
if response.status_code == 302 or 'dashboard' in response.url:
|
||
|
|
print("登录成功!")
|
||
|
|
|
||
|
|
# 测试访问仪表板
|
||
|
|
print("测试访问仪表板...")
|
||
|
|
dashboard_response = session.get('http://localhost:5000/dashboard')
|
||
|
|
print(f"仪表板状态: {dashboard_response.status_code}")
|
||
|
|
|
||
|
|
if dashboard_response.status_code == 200:
|
||
|
|
print("成功访问仪表板!")
|
||
|
|
else:
|
||
|
|
print("访问仪表板失败!")
|
||
|
|
else:
|
||
|
|
print(f"登录失败: {response.status_code}")
|
||
|
|
print(response.text)
|