In the interface Automation test, always encountered, due to the connection timeout and other errors caused by the interface script failed.
The Official method:
Max_retries=5 Error retry 5 times
Note that this only retries for DNS, connection errors.
from Import Httpadapter = requests. Session () s.mount ('http//', Httpadapter (max_retries=5)) S.mount ( 'https://', Httpadapter (max_retries=5)) s.get (' https://www.baidu.com')
Self-authoring Decorator One
fromRequests.exceptionsImportConnectionerrorImportRequestsdefRetry (* *kw):defWar (func):defW (*args,**Kwargs):Try: Ret= Func (*args,**Kwargs)exceptconnectionerror:kw['Renum'] = Int (kw['Renum'])-1ifkw['Renum'] >=0:Printkw['Renum'] RET= W (*args,**Kwargs)Else: Ret=ConnectionerrorreturnretreturnWreturnWar
Self-Authoring Decorator II
fromRequests.exceptionsImportConnectionerrordefRetry (* *kw):defWrapper (func):def_wrapper (*args,**Kwargs): raise_ex=None for_inchRange (kw['Renum']): Print _ Try: returnFunc (*args,**Kwargs)exceptConnectionerror as EX:RAISE_EX=ex#Raise RAISE_EX return_wrapperreturnWrapper
How to use: Renum = 5 means that connectionerror can be retried up to 5 times.
@retry (renum=5)def demo (): raise connectionerror
Summarize:
1. Writing adorners is actually not that difficult, just master the method. This is a reference to the article I wrote about adorners.
2. The pre-cooked explanation of the adorner is to do something after the function. Through this we can do a lot.
3. About the request connection error, retry, adorner; the principle is to do a loop, as long as the capture of a connectionerror error, enter the next cycle
Called, and returns the function directly whenever there is a right time.
QQ Technology Group, look forward to your joining:
python| Test | Technology Group: 563227894
python| Test | Technology Group: 563227894
python| Test | Technology Group: 563227894
Writing adorners to implement Python request error retry function