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!