Writing adorners to implement Python request error retry function

Source: Internet
Author: User
Tags wrapper

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.