1 # -*- coding:utf-8 -*- 2 import requests,json 3 4 class RunMain(): 5 # def __init__(self,url,method,data=None,headers=None): 6 # self.res = self.run_main(url,method,data,headers) 7 8 '''post接口封装,传入url,头部信息和主体信息,没有的时候默认为空''' 9 def send_post(url,data=None,headers=None): 10 # #返回json格式 11 # res = requests.post(url=url,json=data,headers=headers).json() 12 # return json.dumps(res,indent=3,sort_keys=True) 13 #返回text格式 14 res = requests.post(url=url, json=data, headers=headers) 15 return res.json() 16 17 #get接口封装,传入url,头部信息和主体信息,没有的时候默认为空 18 def send_get(url,data=None,headers=None): 19 # res = requests.get(url=url,headers=headers,json=data).json() 20 # return json.dumps(res,indent=3,sort_keys=True) 21 res = requests.get(url=url, json=data, headers=headers) 22 return res.json() 23 24 def run_main(self,url,method,data=None,headers=None): 25 #如果method等于post,则是post接口,否则是get接口 26 res = None 27 if method == "POST": 28 res = RunMain.send_get(url,data,headers) 29 else: 30 res = RunMain.send_post(url,data,headers) 31 return res 32 if __name__ == '__main__': 33 run = RunMain 34 url = '' 35 headers = { 36 'token': '34F0CE94887E4B88954C30D63CC19700', 37 'timestamp': '1', 38 'Content-Type': 'application/json', 39 'password': '123', 40 'account': 't4' 41 } 42 data = { 43 "city": "35", 44 "packageCode": "P000001" 45 } 46 run = RunMain(url,'post',data,headers) 47 print(run.res) 48 # print(run.run_main(url,data,'POST',headers))