def sanitize_redirect(host, redirect_to):
 if redirect_to:
  try:
   netloc = (urlparse(redirect_to)[1] or host)
  except (TypeError, AttributeError):
   pass
  else:
   if (netloc == host):
    return redirect_to

def sanitize_redirect ( host , redirect_to ) : if redirect_to : try : netloc = ( urlparse ( redirect_to ) [ 1 ] or host ) except ( TypeError , AttributeError ) : pass else : if ( netloc == host ) : return redirect_to
given the hostname and an untrusted url to redirect to .