python_发送请求类
一、发送请求类
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(‘登录失败‘)
相关推荐
pythonxuexi 2020-07-19
ciqingloveless 2020-06-19
guangmingsky 2020-06-01
bizercsdn 2020-05-10
小方哥哥 2020-05-10
flowerCSDN 2020-04-21
晓杰0 2020-04-20
txlCandy 2020-04-20
Airuio 2020-02-20
猛禽的编程艺术 2020-02-02
laityc 2020-01-01
jacktangj 2020-01-07
zhglinux 2019-12-25
jacktangj 2019-12-15
heideyanmou 2019-03-21
sharkandshark 2012-03-20
orlandowww 2019-06-26
Lenskit 2019-06-27