python腳本請求數(shù)量達(dá)到上限,http請求重試問題
python請求數(shù)量達(dá)到上限,http請求重試
由于在內(nèi)網(wǎng)發(fā)送http請求同一個token會限制次數(shù),所以很容易達(dá)到網(wǎng)關(guān)流量上限。
業(yè)務(wù)中使用了多線程并發(fā),一個線程發(fā)起一次http請求,得到正確結(jié)果后返回。
這里采用的策略是,如果解析出來達(dá)到流量上限,那么該線程休眠一段時間,然后重試請求,如果還是失敗,那么繼續(xù)休眠,每次休眠的時間隨著重試輪次增加:
# 探測是否觸及網(wǎng)關(guān)流量上限
def probe_func(m_url, m_headers, m_json, m_timeout):
json_rep = requests.post(url = m_url,
headers = m_headers,
json = m_json,
timeout = m_timeout)
zhiyan_data = json_rep.json()
if(zhiyan_data['code'] != 0):
return None
else:
return json_rep
# 解析數(shù)據(jù)包,不涉及probe_func中的檢測內(nèi)容
def parse(json_rep, room_name, metric_name):
if json_rep == None:
logging.info(room_name + " json_rep == None")
return 0
if (json_rep.content and json_rep.status_code != 204 and json_rep.headers["content-type"].strip().startswith("application/json")):
zhiyan_data = json_rep.json()
if len(zhiyan_data['data']) == 0:
logging.warning(zhiyan_data['日志信息拉取無結(jié)果'])
return 0
else:
res = zhiyan_data['data']['chart_info'][0]['key_data_list'][3]['current']
logging.info(room_name + str(res))
if str(res) == "None":
logging.warning(room_name + ":拉取zhiyan_data:" + metric_name + " 出現(xiàn)了問題,拉取數(shù)據(jù)為None")
return 0
else:
return res
else:
return 0
# 具有可靠性地獲取數(shù)據(jù)
def request_post_reliable(m_url, m_headers, m_json, m_timeout):
sleep_time_s = 1
sleep_time_max = 60
res = probe_func(m_url, m_headers, m_json, m_timeout)
# 如果探測失敗則線程睡眠一段時間后再嘗試
while (res == None):
logging.info("探測失敗,線程睡眠"+str(sleep_time_s)+"秒")
time.sleep(sleep_time_s)
tmp = sleep_time_s * 2
if tmp < sleep_time_max:
sleep_time_s = tmp
else:
sleep_time_s = sleep_time_max
logging.info("睡眠結(jié)束,線程重新探測")
res = probe_func(m_url, m_headers, m_json, m_timeout)
# 直到探測成功,返回正確結(jié)果
return res
python請求http/https時設(shè)置失敗重試次數(shù)
使用Python的requests庫時,默認(rèn)是沒有失敗時重試請求的,通過下面的方式可以支持重試請求
設(shè)置請求時的重試規(guī)則
import requests
from requests.adapters import HTTPAdapter
s = requests.Session()
a = HTTPAdapter(max_retries=3)
b = HTTPAdapter(max_retries=3)
#將重試規(guī)則掛載到http和https請求
s.mount('http://', a)
s.mount('https://', b)
請求Url
上面設(shè)置完畢后,通過改Session的請求就可以支持失敗重試
r = s.get('http://api.map.baidu.com/geocoder?location=39.90733345,116.391244079988&output=json')
# 返回的狀態(tài)碼
r.status_code
# 響應(yīng)內(nèi)容,中文為utf8編碼
r.content
# 響應(yīng)的字符串形式,中文為unicode編碼
r.text
# 響應(yīng)頭中的編碼
r.encoding
# 響應(yīng)頭信息
r.headers
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python_tkinter彈出對話框創(chuàng)建2
這篇文章主要介紹了python_tkinter彈出對話框創(chuàng)建,上以篇文章我們簡單的對對話框創(chuàng)建做了簡單介紹,本文將繼續(xù)更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-03-03
基于python+selenium自動健康打卡的實(shí)現(xiàn)代碼
這篇文章主要介紹了基于python+selenium自動健康打卡,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
深度解析Python中遞歸下降解析器的原理與實(shí)現(xiàn)
在編譯器設(shè)計、配置文件處理和數(shù)據(jù)轉(zhuǎn)換領(lǐng)域,遞歸下降解析器是最常用且最直觀的解析技術(shù),本文將詳細(xì)介紹遞歸下降解析器的原理與實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-08-08
PyTorch中的參數(shù)類torch.nn.Parameter()詳解
這篇文章主要給大家介紹了關(guān)于PyTorch中torch.nn.Parameter()的相關(guān)資料,要內(nèi)容包括基礎(chǔ)應(yīng)用、實(shí)用技巧、原理機(jī)制等方面,文章通過實(shí)例介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
Python中xrange與yield的用法實(shí)例分析
這篇文章主要介紹了Python中xrange與yield的用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了range和xrange功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2017-12-12

