python_发送请求类,,一、发送请求类imp
python_发送请求类,,一、发送请求类imp
一、发送请求类
import requestsclass 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 = reqif __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(‘登录失败‘)
python_发送请求类
相关内容
- python处理数据库的返回数据,,上代码:import
- Python版本的谷歌S2空间索引算法,索引的算法,参考htt
- python基础知识(字符串),python字符串比较,定义字符串
- python opencv 去除水印,python去除水印,#!/usr/bin
- python 省略号 三个点...的含义,省略号的含义有哪些,
- 安装Python3后,centos使用yum报错,centos7yum,题记在之前的文
- Python内置函数,Python常用内置函数,abs() 获取绝对
- python中的not的意思,python中的not,python中的no
- Python入门学习(二)——函数篇,,1.函数要调用一个函
- python--关键字参数/可变参数--内置函数--时间模块--常用
评论关闭