日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

【CyberSecurityLearning 附】python3-requests模块

發(fā)布時(shí)間:2025/3/15 python 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CyberSecurityLearning 附】python3-requests模块 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

requests模塊

模塊說(shuō)明

速查

HTTP方法

requests 模塊中的http方法

參數(shù)

對(duì)象方法

模塊入門

導(dǎo)入模塊

發(fā)送簡(jiǎn)潔請(qǐng)求?

相關(guān)方法

相關(guān)操作

定制頭部

超時(shí)

GET 傳參

POST 傳參

上傳文件

重定向

關(guān)于cookies


?

requests模塊

模塊說(shuō)明

requests是使用Apache2 licensed許可證的HTTP庫(kù)。
用python編寫(xiě)。
比urllib2模塊更簡(jiǎn)潔。

Request支持HTTP連接保持和連接池,支持使用cookie保持會(huì)話,支持文件上傳,支持自動(dòng)響應(yīng)的編碼,支持國(guó)際化的URL和POST數(shù)據(jù)自動(dòng)編碼。
內(nèi)置模塊的基礎(chǔ)上進(jìn)行了高度的封裝,從而使python進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),變得人性化,使用Requests可以輕而易舉的完成瀏覽器可有的任何操作
現(xiàn)代,國(guó)際化,友好。
requests會(huì)自動(dòng)實(shí)現(xiàn)持久連接keep-alive。

速查

HTTP方法

requests模塊都支持哪些方法呢?見(jiàn)下。

GET

獲取資源

POST

傳輸實(shí)體主體

PUT

傳輸文件

HEDA

獲得響應(yīng)報(bào)文首部

DELETE

刪除文件

OPTIONS

查詢支持的方法

TRACK

追蹤路徑

CONNECT

要求用隧道協(xié)議連接代理

LINK

建立呵資源之間的連接

UNLINK

斷開(kāi)連接

requests 模塊中的http方法

如果我們想使用上面那些方法,我們?cè)趐ython里面怎么使用呢?見(jiàn)下。

res = requests.get()??? 發(fā)送get請(qǐng)求???? res就是response響應(yīng)
res = requests.post()
res = requests.put()
res = requests.delete()
res = requests.head()
res = requests.options()

?

?

?

?

?

參數(shù)

GET參數(shù)

params

HTTP頭部

headers

POST參數(shù)

data

文件

files

Cookies

cookies

重定向處理

allow_redirects = False/True

超時(shí)

timeout

證書(shū)驗(yàn)證

verify = False/True

工作流(延遲下載)

stream = False/True

事件掛鉤

hooks = dict(response=)

身份驗(yàn)證

auth =

代理

proxies =

?

對(duì)象方法

URL

.url

text

.text

編碼

.excoding|.encoding=

響應(yīng)內(nèi)容

.content

Json 解碼器

.json

原始套接字響應(yīng)

.raw|.raw.read()

歷史響應(yīng)代碼

.history

拋出異常

.raise_for_status()

查看服務(wù)器響應(yīng)頭

.headers

查看客戶端請(qǐng)求頭

.request.headers

查看Cookie

.cookies

身份驗(yàn)證

.auth=

更新

.update

解析連接字頭

.links[]

?

模塊入門

導(dǎo)入模塊

import requests

測(cè)試:我們發(fā)送的http請(qǐng)求:

發(fā)送簡(jiǎn)潔請(qǐng)求?

發(fā)送get 請(qǐng)求

res = requests.get("http://192.168.1.200/php/get.php")????
會(huì)把所有的get請(qǐng)求放到res這個(gè)變量里面去,會(huì)把請(qǐng)求得到的響應(yīng)的所有內(nèi)容全放到res這個(gè)對(duì)象中去,然后通過(guò)這個(gè)res去獲取正文

相關(guān)方法

獲取響應(yīng)正文

res.txt

獲取響應(yīng)狀態(tài)碼

res.status_code

獲取響應(yīng)編碼

res.encoding

以二進(jìn)制方式獲取相應(yīng)正文

res.content

獲取響應(yīng)頭

res.headers

獲取提交的URL(包括GET 參數(shù))

res.url

獲取發(fā)送到服務(wù)器的頭信息

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'

>>>

------------------

?

相關(guān)操作

定制頭部

@ 就是重新定義User-Agent信息

import requests #導(dǎo)入requests模塊 url = "http://192.168.3.135/pytest/get.php" #請(qǐng)求的鏈接 header = {"User-Agent":"Waffle"} #重新定義HTTP頭部 res = requests.get(url=url,headers=header) #res就是去發(fā)送這樣一個(gè)請(qǐng)求 (等號(hào)前面的url是requests一個(gè)GET方法里面的參數(shù),等號(hào)后面的url是請(qǐng)求的鏈接。header是我們定義的字典,headers是get方法的一個(gè)參數(shù)) print(res.request.headers) # res是響應(yīng)(響應(yīng)的原來(lái)這個(gè)請(qǐng)求的頭部信息)

超時(shí)

-----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"}? #我們?yōu)榱私y(tǒng)一,所有的參數(shù)都放在字典里面
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>

文件上傳測(cè)試

</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__獲取當(dāng)前php腳本所在目錄

//echo $path;

if(move_uploaded_file($tmp_path,$path)){

//move_uploaded_file(參數(shù)1,參數(shù)2);將上傳上來(lái)的緩存文件的目錄(參數(shù)1)保存到參數(shù)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的路徑是我本地的路徑,冒號(hào)前面是文件上傳那個(gè)表單,input標(biāo)簽的名字 postData={"userSubmit":"submit"} #userSubmit是那個(gè)表單按鈕的值 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!

>>>

----------
?

關(guān)于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)

?

總結(jié)

以上是生活随笔為你收集整理的【CyberSecurityLearning 附】python3-requests模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。