吾生志愿积善读书,不敢望名声动地,不敢望富贵惊天,不敢望一言定国,不敢望七步成篇,不敢望珊瑚树高百尺,不敢望琉璃瓦盖千层,但愿父母康健夫妻偕老,兄友弟恭子孝孙贤,无荣无辱骨肉团圆。
00 前言
Requests 对象的 GET/POST 方法都会返回一个 Response 对象,这个对象里面存的是服务器返回的所有信息,包括响应头,响应状态码等。
01 环境
Phpstudy 搭建的一个简易网页
① 源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>postfind injection</title>
</head>
<body style=" margin-top:70px; font-size:23px; text-align:center">
<font size="5" face="Times">搜索型SQL注入测试</font>
<br><br>
<a href="getfind-injection.php">GET型注入</a>
<a href="postfind-injection.php">POST型注入</a>
<br><hr>
<form action="" method="POST">
用户:<input type="text" name="username" value=""><br>
密码:<input type="text" name="password" value=""><br>
<input type="submit" name="commit" value="submit">
</form>
<?php
$username = addslashes($_POST['username']);
$password = addslashes($_POST['password']);
$conn = mysql_connect('localhost','root','root');
mysql_select_db("test_user",$conn);
$sql = "SELECT * FROM login WHERE username = '$username' and password = '$password'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
echo "<br>登录成功!!!<br><br>";
echo "用户ID:".$row['Id']."<br>";
echo "用户:" .$row['username']."<br>";
echo "密码:".$row['password']."<br>";
}
mysql_close($conn);
echo "<hr>";
echo "当前执行的SQL语句:"."<br>";
echo $sql;
?>
</body>
</html>
② 界面
02 测试
① Response 对象属性
属性 | 说明 |
---|---|
r.status_code | HTTP 请求的返回状态,1xx 表示信息提示;2xx 表示连接成功;3xx 表示重定向;4xx 表示客户端错误,5xx 表示服务端错误 |
r.text | HTTP 响应内容的字符串形式,URL 对应的页面内容 |
r.encoding | 从HTTP header 中猜测的响应内容编码方式 |
r.apparent_encoding | 从内容分析出的响应内容的编码方式(备选编码方式) |
r.content | HTTP 响应内容的二进制形式 |
r.headers | HTTP 响应内容的头部内容 |
1. python 源码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
url = "http://zhutou.com/sql-test/postfind-injection.php"
res = requests.post(url)
re_text = res.text
re_content = res.content
print('响应内容编码(HTTP头):',res.encoding)
print('响应内容编码(内容):',res.apparent_encoding)
print('请求状态:',res.status_code)
print('text 方法返回的形式:',type(re_text))
print('content 方法返回的形式:',type(re_content))
print('HTTP响应内容的头部:',res.headers)
# 利用 for 循环输出字典所有键值对
for i in res.headers.items():
print(i)
2. 输出结果
响应内容编码(HTTP头): ISO-8859-1
响应内容编码(内容): utf-8
请求状态: 200
text 方法返回的形式: <class 'str'>
content 方法返回的形式: <class 'bytes'>
HTTP响应内容的头部: {'Connection': 'Keep-Alive', 'X-Powered-By': 'PHP/5.2.17', 'Keep-Alive': 'timeout=5, max=100', 'Date': 'Tue, 24 Mar 2020 14:31:57 GMT', 'Content-Length': '709', 'Server': 'Apache/2.2.25 (Win32) mod_ssl/2.2.25 OpenSSL/0.9.8y PHP/5.2.17', 'Content-Type': 'text/html'}
('Date', 'Tue, 24 Mar 2020 14:31:57 GMT')
('Server', 'Apache/2.2.25 (Win32) mod_ssl/2.2.25 OpenSSL/0.9.8y PHP/5.2.17')
('X-Powered-By', 'PHP/5.2.17')
('Content-Length', '709')
('Keep-Alive', 'timeout=5, max=100')
('Connection', 'Keep-Alive')
('Content-Type', 'text/html')
② 带参 POST 请求
Response 对象返回的网页源代码会存在 content/text 两个方法中,两者的区别是 text 返回的是 Unicode 型的数据,content 返回的是 bytes 型也就是二进制的数据,使用 str(content) 或者content.decode() 才能正常显示中文,将 Response 对象返回的内容进行 utf-8 编码(r.encoding=‘utf-8’)r.text 才能正常输出
1. python 源码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
url = "http://zhutou.com/sql-test/postfind-injection.php"
data = {'username':'admin','password':'admin','commit':'submit'}
r = requests.post(url,data)
temp = r.content
if r.status_code == 200:
print('访问成功!')
'''
# 使用 r.content
if str(temp).find('用户名'):
print('账密正确!')
'''
# 使用 r.text
r.encoding = 'utf-8' # 使用 text 前必须对 Response 进行 utf-8 编码
result = r.text
if r.text.find('登录成功') > 0:
print('账密正确!')
else:
print('账密错误!')
2. 输出结果
访问成功!
账密正确!
猪头
2020.3.25