import requests
def custom_response_handler(response, *args, **kwargs):
if response.status_code != 200:
print(response.request.url + " " + response.text)
raise RuntimeError(response.text)
json_data = response.json()
if json_data.get('errcode') != 0:
print("🚨🚨🚨 Error 🚨🚨🚨️")
print(response.request.url + " " + json_data.get('errmsg'))
raise RuntimeError(json_data.get('errmsg'))
else:
pass
return response
class CustomSession(requests.Session):
def __init__(self):
super(CustomSession, self).__init__()
def request(self, method, url, **kwargs):
url = 'https://www.xxx.com' + url
kwargs.setdefault('timeout', 3)
kwargs.setdefault('headers', {}).update(...)
return super(CustomSession, self).request(method, url, **kwargs)
my_session = CustomSession()
my_session.verify = False
my_session.hooks['response'].append(custom_response_handler)
本文使用MdSync工具发布!