国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python中requests小技巧

2020-01-04 17:08:47
字體:
來源:轉載
供稿:網友

關于  Python requests ,在使用中,總結了一些小技巧把,記錄下。

1:保持請求之間的Cookies,我們可以這樣做。

python中requests模塊,python中requests下載,python中的requests

2:請求時,會加上headers,一般我們會寫成這樣

python中requests模塊,python中requests下載,python中的requests

唯一不便的是之后的代碼每次都需要這么寫,代碼顯得臃腫,所以我們可以這樣:

python中requests模塊,python中requests下載,python中的requests

3:默認requests請求失敗后不會重試,但是我們跑case時難免遇到一些網絡或外部原因導致case失敗,我們可以在Session實例上附加HTTPAdapaters 參數,增加失敗重試次數。

python中requests模塊,python中requests下載,python中的requests

這樣,之后的請求,若失敗,重試3次。

4:重定向

網絡請求中可能會遇到重定向,我們需要一次處理一個請求,可以把重定向禁止。

python中requests模塊,python中requests下載,python中的requests

5:post請求提交json格式數據時,一般先要把python對象轉換成json對象。可能很多時候是這樣寫:

python中requests模塊,python中requests下載,python中的requests

其實post有一個默認參數json,可以直接簡寫成:

python中requests模塊,python中requests下載,python中的requests

6:寫接口請求,debug時,會需要看下代碼請求的詳細信息,當然我們可以使用fiddler來查看,其實我們自己也可以在代碼這樣獲取debug信息 *

python中requests模塊,python中requests下載,python中的requests

7:使用grequests實現異步請求。

pip install grequests

python中requests模塊,python中requests下載,python中的requests

8:發送自定義cookies

我們使用Session實例來保持請求之間的cookies,但是有些特殊情況,需要使用自定義的cookies

我們可以這樣

python中requests模塊,python中requests下載,python中的requests

9:需求確定前后端并行設計時,這時 測試 并沒有辦法調用接口,如何做接口測試呢?我們可以使用mock或者是httpretty

python中requests模塊,python中requests下載,python中的requests

詳細使用可以查看API_DOC

10:統計一個API請求花費的時間,我們可以使用如下方法

python中requests模塊,python中requests下載,python中的requests

11:設置請求超時

python中requests模塊,python中requests下載,python中的requests

關于 Python ,requests的小技巧,就是這些。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南郑县| 临沧市| 东明县| 和田县| 盐源县| 偃师市| 安顺市| 沁阳市| 囊谦县| 临颍县| 佳木斯市| 黔西县| 类乌齐县| 洪泽县| 泸溪县| 巴彦县| 双江| 安塞县| 鲁山县| 凤阳县| 台湾省| 思茅市| 晋州市| 新龙县| 荣成市| 阿拉尔市| 双柏县| 巴林右旗| 武鸣县| 白银市| 虞城县| 即墨市| 辰溪县| 巴中市| 凤冈县| 南丰县| 深水埗区| 呼玛县| 镇江市| 林州市| 常德市|