【CyberSecurityLearning 附】python3-requests模块
目錄
requests模塊
模塊說明
速查
HTTP方法
requests 模塊中的http方法
參數
對象方法
模塊入門
導入模塊
發送簡潔請求?
相關方法
相關操作
定制頭部
超時
GET 傳參
POST 傳參
上傳文件
重定向
?
requests模塊
模塊說明
requests是使用Apache2 licensed許可證的HTTP庫。
用python編寫。
比urllib2模塊更簡潔。
Request支持HTTP連接保持和連接池,支持使用cookie保持會話,支持文件上傳,支持自動響應的編碼,支持國際化的URL和POST數據自動編碼。
內置模塊的基礎上進行了高度的封裝,從而使python進行網絡請求時,變得人性化,使用Requests可以輕而易舉的完成瀏覽器可有的任何操作。
現代,國際化,友好。
requests會自動實現持久連接keep-alive。
速查
HTTP方法
requests模塊都支持哪些方法呢?見下。
| GET | 獲取資源 |
| POST | 傳輸實體主體 |
| PUT | 傳輸文件 |
| HEDA | 獲得響應報文首部 |
| DELETE | 刪除文件 |
| OPTIONS | 查詢支持的方法 |
| TRACK | 追蹤路徑 |
| CONNECT | 要求用隧道協議連接代理 |
| LINK | 建立呵資源之間的連接 |
| UNLINK | 斷開連接 |
requests 模塊中的http方法
如果我們想使用上面那些方法,我們在python里面怎么使用呢?見下。
| res = requests.get()??? 發送get請求???? res就是response響應 |
| res = requests.post() |
| res = requests.put() |
| res = requests.delete() |
| res = requests.head() |
| res = requests.options() |
?
?
?
?
?
參數
| GET參數 | params |
| HTTP頭部 | headers |
| POST參數 | data |
| 文件 | files |
| Cookies | cookies |
| 重定向處理 | allow_redirects = False/True |
| 超時 | timeout |
| 證書驗證 | verify = False/True |
| 工作流(延遲下載) | stream = False/True |
| 事件掛鉤 | hooks = dict(response=) |
| 身份驗證 | auth = |
| 代理 | proxies = |
?
對象方法
| URL | .url |
| text | .text |
| 編碼 | .excoding|.encoding= |
| 響應內容 | .content |
| Json 解碼器 | .json |
| 原始套接字響應 | .raw|.raw.read() |
| 歷史響應代碼 | .history |
| 拋出異常 | .raise_for_status() |
| 查看服務器響應頭 | .headers |
| 查看客戶端請求頭 | .request.headers |
| 查看Cookie | .cookies |
| 身份驗證 | .auth= |
| 更新 | .update |
| 解析連接字頭 | .links[] |
?
模塊入門
導入模塊
import requests
測試:我們發送的http請求:
發送簡潔請求?
發送get 請求
res = requests.get("http://192.168.1.200/php/get.php")????
會把所有的get請求放到res這個變量里面去,會把請求得到的響應的所有內容全放到res這個對象中去,然后通過這個res去獲取正文
相關方法
獲取響應正文
res.txt
獲取響應狀態碼
res.status_code
獲取響應編碼
res.encoding
以二進制方式獲取相應正文
res.content
獲取響應頭
res.headers
獲取提交的URL(包括GET 參數)
res.url
獲取發送到服務器的頭信息
res.request.headers
例如:
----------get.php
<?php
var_dump($_GET);
?>
----------------------
-------------------
>>> import requests
>>> res = requests.get("http://192.168.1.200/php/get.php")
>>> res.text
'array(0) {\n}\n'
>>> res.status_code
200
>>> res.encoding
'ISO-8859-1'
>>> res.content
b'array(0) {\n}\n'
>>> res.headers
{'Date': 'Thu, 14 May 2020 00:57:37 GMT', 'Server': 'Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45', 'X-Powered-By': 'PHP/5.4.45', 'Content-Length': '13', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}
>>> res.request.headers
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> res.url
'http://192.168.1.200/php/get.php'
>>>
------------------
?
相關操作
定制頭部
@ 就是重新定義User-Agent信息
import requests #導入requests模塊 url = "http://192.168.3.135/pytest/get.php" #請求的鏈接 header = {"User-Agent":"Waffle"} #重新定義HTTP頭部 res = requests.get(url=url,headers=header) #res就是去發送這樣一個請求 (等號前面的url是requests一個GET方法里面的參數,等號后面的url是請求的鏈接。header是我們定義的字典,headers是get方法的一個參數) print(res.request.headers) # res是響應(響應的原來這個請求的頭部信息)超時
-----timeout.php---
---------------------------
import requests
url="http://192.168.3.135/pytest/timeout.php"
try:
??? res=requests.get(url=url,timeout=3)
??? print(res.text)
except Exception as e:
??? print("TimeOut!")
GET 傳參
import requests
url="http://192.168.3.135/pytest/get.php"
getPara={"name":"Waffle","pwd":"123456"}? #我們為了統一,所有的參數都放在字典里面
res=requests.get(url=url,params=getPara)
print(res.text)
print(res.url)
POST 傳參
import requests
url="http://192.168.3.135/pytest/post.php"
postData={"name":"Waffle","pwd":"123456"}
res=requests.post(url=url,data=postData)
print(res.text)
上傳文件
----------upfile.php
<html>
<meta charset="utf-8">
<h1>
文件上傳測試
</h1>
<form
action=""
method="post"
enctype="multipart/form-data"
>
<input type="file" name="userUpFile">
<input type="submit" name="userSubmit" value="上傳">
</form>
</html>
<hr />
<?php
echo "<pre>";
if(isset($_POST['userSubmit'])){
var_dump($_FILES);
$tmp_path=$_FILES['userUpFile']['tmp_name'];
$path=__DIR__."\\".$_FILES['userUpFile']['name'];//__DIR__獲取當前php腳本所在目錄
//echo $path;
if(move_uploaded_file($tmp_path,$path)){
//move_uploaded_file(參數1,參數2);將上傳上來的緩存文件的目錄(參數1)保存到參數2目錄下
echo "upfile success!";
echo "<br />".$_FILES['userUpFile']['name'];
}else{
echo "upfile failed";
}
}
?>
---------------------------------
import requests url="http://192.168.3.135/pytest/upfile.php" upFile={"userUpFile":open("E:\pass.txt","rb")} #open的路徑是我本地的路徑,冒號前面是文件上傳那個表單,input標簽的名字 postData={"userSubmit":"submit"} #userSubmit是那個表單按鈕的值 res=requests.post(url=url,files=upFile,data=postData) print(res.text)?
?
重定向
------redirect.php
<?php
header('location:./get.php');
echo "This is redirect.php!";
?>
---------------------------
import requestsurl = "http://192.168.1.200/php/redirect.php"res = requests.get(url=url)print(res.text) print(res.history)print('\n')res = requests.get(url=url,allow_redirects=False)print(res.headers) print(res.text)---------
array(0) {
}
[<Response [302]>]
{'Date': 'Thu, 14 May 2020 09:02:34 GMT', 'Server': 'Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45', 'X-Powered-By': 'PHP/5.4.45', 'location': './get.php', 'Content-Length': '21', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}
This is redirect.php!
>>>
----------
?
關于cookies
-----cookie.php
<?php
var_dump($_COOKIE);
?>
--------------------
import requests url = "http://192.168.3.135/pytest/cookie.php" Coo = {"name":"Waffle"} res = requests.get(url=url,cookies=Coo) print(res.text)?
總結
以上是生活随笔為你收集整理的【CyberSecurityLearning 附】python3-requests模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新工科背景下的大数据体系建设探析
- 下一篇: 作者:王亮(1975-),男,中国科学院