1. 程式人生 > >python+requests介面自動化--請求方法封裝

python+requests介面自動化--請求方法封裝

 1 import requests
 2 from common.logger import Log
 3 from common import cof
 4 from common import base
 5 
 6 
 7 class MyHttpservice(object):
 8 
 9     def __init__(self):
10         self.url = cof.get_host1()
11         self.log = Log()
12 
13     def get(self,**kwargs):
14         '''封裝get方法'''
15 params = kwargs.get("params") 16 headers = kwargs.get("headers") 17 try: 18 res = requests.get(self.url,params=params,headers=headers) 19 self.log.info("測試介面:%s" %base.get_url(route)) 20 self.log.info("響應的內容:%s" %res.json()) 21 self.log.info("
返回的狀態碼:%s" % res.status_code) 22 return res 23 except Exception as e: 24 print("get請求錯誤: %s" %e) 25 26 def post(self,url,**kwargs): 27 '''封裝post方法''' 28 params = kwargs.get("params") 29 data = kwargs.get("data") 30 json = kwargs.get("
json") 31 files = kwargs.get("files") 32 try: 33 res = requests.post(url,params=params,data=data,json=json,files=files) 34 self.log.info("響應的內容:%s" %res.json()) 35 self.log.info("返回的狀態碼:%s" % res.status_code) 36 return res 37 except Exception as e: 38 print("post請求錯誤: %s" %e) 39 40 def delete(self,url,**kwargs): 41 '''封裝delete方法''' 42 params = kwargs.get("params") 43 data = kwargs.get("data") 44 json = kwargs.get("json") 45 files = kwargs.get("files") 46 try: 47 res = requests.delete(url,params=params,data=data,json=json,files=files) 48 self.log.info("響應的內容:%s" %res.json()) 49 self.log.info("返回的狀態碼:%s" % res.status_code) 50 return res 51 except Exception as e: 52 print("post請求錯誤: %s" %e) 53 54 def put(self, url, **kwargs): 55 '''封裝put方法''' 56 params = kwargs.get("params") 57 data = kwargs.get("data") 58 json = kwargs.get("json") 59 files = kwargs.get("files") 60 try: 61 res = requests.put(url, params=params, data=data, json=json, files=files) 62 self.log.info("響應的內容:%s" % res.json()) 63 self.log.info("返回的狀態碼:%s" % res.status_code) 64 return res 65 except Exception as e: 66 print("post請求錯誤: %s" % e)