def handle_redirects(func):
 MAX_REDIRECTS = 5
 @functools.wraps(func)
 def wrapped(self, method, url, body, headers):
  for _ in xrange(MAX_REDIRECTS):
   try:
    return func(self, method, url, body, headers)
   except exception.RedirectException as redirect:
    if (redirect.url is None):
     raise exception.InvalidRedirect()
    url = redirect.url
  raise exception.MaxRedirectsExceeded(redirects=MAX_REDIRECTS)
 return wrapped

def handle_redirects ( func ) : MAX_REDIRECTS = 5 @ functools . wraps ( func ) def wrapped ( self , method , url , body , headers ) : for _ in xrange ( MAX_REDIRECTS ) : try : return func ( self , method , url , body , headers ) except exception . RedirectException as redirect : if ( redirect . url is None ) : raise exception . InvalidRedirect ( ) url = redirect . url raise exception . MaxRedirectsExceeded ( redirects = MAX_REDIRECTS ) return wrapped
wrap the _do_request function to handle http redirects .