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

歡迎訪問 生活随笔!

生活随笔

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

python

python安装requests库超时_【Python 库】requests 详解超时和重试

發(fā)布時間:2024/7/5 python 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python安装requests库超时_【Python 库】requests 详解超时和重试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

網(wǎng)絡(luò)請求不可避免會遇上請求超時的情況,在 requests 中,如果不設(shè)置你的程序可能會永遠(yuǎn)失去響應(yīng)。

超時又可分為連接超時和讀取超時。

連接超時

連接超時指的是在你的客戶端實(shí)現(xiàn)到遠(yuǎn)端機(jī)器端口的連接時(對應(yīng)的是connect()),Request 等待的秒數(shù)。

import time

import requests

url = 'http://www.google.com.hk'

print(time.strftime('%Y-%m-%d %H:%M:%S'))

try:

html = requests.get(url, timeout=5).text

print('success')

except requests.exceptions.RequestException as e:

print(e)

print(time.strftime('%Y-%m-%d %H:%M:%S'))

因?yàn)?google 被墻了,所以無法連接,錯誤信息顯示 connect timeout(連接超時)。

2018-12-14 14:38:20

HTTPConnectionPool(host='www.google.com.hk', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(, 'Connection to www.google.com.hk timed out. (connect timeout=5)'))

2018-12-14 14:38:25

就算不設(shè)置,也會有一個默認(rèn)的連接超時時間(我測試了下,大概是21秒)。

讀取超時

讀取超時指的就是客戶端等待服務(wù)器發(fā)送請求的時間。(特定地,它指的是客戶端要等待服務(wù)器發(fā)送字節(jié)之間的時間。在 99.9% 的情況下這指的是服務(wù)器發(fā)送第一個字節(jié)之前的時間)。

簡單的說,連接超時就是發(fā)起請求連接到連接建立之間的最大時長,讀取超時就是連接成功開始到服務(wù)器返回響應(yīng)之間等待的最大時長。

如果你設(shè)置了一個單一的值作為 timeout,如下所示:

r = requests.get('https://github.com', timeout=5)

這一 timeout 值將會用作 connect 和 read 二者的 timeout。如果要分別制定,就傳入一個元組:

r = requests.get('https://github.com', timeout=(3.05, 27))

黑板課爬蟲闖關(guān)的第四關(guān)正好網(wǎng)站人為設(shè)置了一個15秒的響應(yīng)等待時間,拿來做說明最好不過了。

import time

import requests

url_login = 'http://www.heibanke.com/accounts/login/?next=/lesson/crawler_ex03/'

session = requests.Session()

session.get(url_login)

token = session.cookies['csrftoken']

session.post(url_login, data={'csrfmiddlewaretoken': token, 'username': 'xx', 'password': 'xx'})

print(time.strftime('%Y-%m-%d %H:%M:%S'))

url_pw = 'http://www.heibanke.com/lesson/crawler_ex03/pw_list/'

try:

html = session.get(url_pw, timeout=(5, 10)).text

print('success')

except requests.exceptions.RequestException as e:

print(e)

print(time.strftime('%Y-%m-%d %H:%M:%S'))

錯誤信息中顯示的是 read timeout(讀取超時)。

2018-12-14 15:20:47

HTTPConnectionPool(host='www.heibanke.com', port=80): Read timed out. (read timeout=10)

2018-12-14 15:20:57

讀取超時是沒有默認(rèn)值的,如果不設(shè)置,程序?qū)⒁恢碧幱诘却隣顟B(tài)。我們的爬蟲經(jīng)常卡死又沒有任何的報錯信息,原因就在這里了。

超時重試

一般超時我們不會立即返回,而會設(shè)置一個三次重連的機(jī)制。

def gethtml(url):

i = 0

while i < 3:

try:

html = requests.get(url, timeout=5).text

return html

except requests.exceptions.RequestException:

i += 1

其實(shí) requests 已經(jīng)幫我們封裝好了。(但是代碼好像變多了...)

import time

import requests

from requests.adapters import HTTPAdapter

s = requests.Session()

s.mount('http://', HTTPAdapter(max_retries=3))

s.mount('https://', HTTPAdapter(max_retries=3))

print(time.strftime('%Y-%m-%d %H:%M:%S'))

try:

r = s.get('http://www.google.com.hk', timeout=5)

return r.text

except requests.exceptions.RequestException as e:

print(e)

print(time.strftime('%Y-%m-%d %H:%M:%S'))

max_retries 為最大重試次數(shù),重試3次,加上最初的一次請求,一共是4次,所以上述代碼運(yùn)行耗時是20秒而不是15秒

2018-12-14 15:34:03

HTTPConnectionPool(host='www.google.com.hk', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(, 'Connection to www.google.com.hk timed out. (connect timeout=5)'))

2018-12-14 15:34:23

相關(guān)博文推薦:

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的python安装requests库超时_【Python 库】requests 详解超时和重试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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