Sample Code for automatically retrying http request upon request failure

Source: Internet
Author: User

Sample Code for automatically retrying http request upon request failure

This document describes an example of an automatic retry when an http request fails.

Requirement

A recently developed project is designed to implement an http request. If the request fails, you need to automatically try it several times and record the cause of the exception for troubleshooting.

Code Implementation

# Automatically reconnect def conn_try_again (function): RETRIES = 0 # Number of RETRIES count = {"num": RETRIES} def wrapped (* args, ** kwargs): try: return function (* args, ** kwargs) except t Exception, err: if count ['num'] <2: count ['num'] + = 1 return wrapped (* args, ** kwargs) else: raise Exception (err) return wrapped @ conn_try_againdef http_request (session, method, url, data, header = header): try: if method = 'get': r = session. get (url, data = data, headers = header) return r elif method = 'post': r = session. post (url, data = data, headers = header) return r else: return None except T Exception as e: logging. exception ('[online_statistics] http request connection error % s' % e)

Summary

The above is all the content of this article about the sample code for an automatic retry of an http request failure. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.