标签:params int www ted 接口自动化 port json method class
Requests库是用Python编写的,基于urllib,采用Apache2 Licensed开源协议的HTTP库
更详细的介绍,请参考https://zhuanlan.zhihu.com/p/34199974
下面讲解get方法和post方法的封装和调用
import requests,json
class RunByMethod():
##post请求方式
def run_post(self,isJson,url,data=None,header=None):
if isJson:
res = requests.post(url=url,json=data,headers=header)
else:
res = requests.post(url=url,data=data,headers=header)
return res.json()
##get请求方式
def run_get(self,isJson,url,data=None,header=None):
if isJson:
res = requests.get(url=url,json=data,headers=header)
else:
res = requests.get(url=url,data=data,headers=header)
return res.json()
##主函数,根据请求方式,调用对应的方法
def run_main(self,method,isJson,url,data=None,header=None):
if method =="POST":
res = self.run_post(isJson,url,data,header)
else:
res = self.run_get(isJson,url,data,header)
return json.dumps(res,ensure_ascii=False,indent=2)
if __name__=="__main__":
rm = RunByMethod()
##get请求方式
base_url = "http://exam.mofing.com/plan/hongbaorain/439124.json"
res = rm.run_main("GET",False,base_url)
print(res)
##post请求方式
base_url2 = "http://www.pingan.com/cms-tmplt/pinganlife/synShopList.do"
params = {"dateUpdated":"2021-01-05"}
res1 = rm.run_main("POST",False,base_url2,params)
print(res1)
标签:params int www ted 接口自动化 port json method class
原文地址:https://www.cnblogs.com/lldk/p/14260111.html