1. 接口自动化框架---封装request(request_handler)
1 import requests 2 3 4 def send_request(url, method='GET', **kwargs): 5 """ 6 发送请求 7 :param url: 8 :param method: 请求方法 9 :param kwargs: 接收request原生请求的关键字参数 10 :return: 响应对象 11 """ 12 method = method.upper() 13 if method == 'GET': 14 res = requests.get(url, **kwargs) 15 elif method == 'POST': 16 res = requests.post(url, **kwargs) 17 elif method == 'PATCH': 18 res = requests.patch(url, **kwargs) 19 20 # 5XX服务器错误提示 21 if res.status_code >= 500: 22 raise ValueError('服务器错误') 23 # 4XX前端错误 24 elif res.status_code >= 400: 25 raise ValueError('前端错误') 26 return res 27 28 29 if __name__ == '__main__': 30 url = 'http://www.baidu.com' 31 headers = {'my_headers': 'aaa'} 32 send_request(url=url, method='get', headers=headers)