一、发送请求类
import requests class MyRequest: def __init__(self,url,method=‘get‘,data=None,headers=None,is_json=False): method = method.lower() self.url = url self.data = data self.headers = headers self.is_json = is_json if hasattr(self,method): getattr(self,method)() #反射 def get(self): try: req = requests.get(self.url,self.data,heasers=self.headers).json() except Exception as e: self.response = {‘error‘:‘接口请求出错%s‘%e} else: self.response = req def post(self): try: if self.is_json: req = requests.post(self.url, json=self.data, heasers=self.headers).json() else: req = requests.post(self.url, self.data, heasers=self.headers).json() except Exception as e: self.response = {‘error‘: ‘接口请求出错%s‘%e} else: self.response = req if __name__ == ‘__main__‘: import jsonpath login = MyRequest(‘ http://127.0.0.1:5000/login‘,data={‘username‘:‘xmb‘,‘password‘:123456}) result = jsonpath.jsonpath(login.response,‘$..session_id‘) if result: print(‘登录成功‘) else: print(‘登录失败‘)
原文地址:https://www.cnblogs.com/xumb/p/11963581.html
时间: 2024-10-29 16:37:23