Encapsulation (Python) according to the interface request type

The request interface package different request types (GET / POST)

. 1  Import Requests
 2  Import JSON
 . 3  
. 4  
. 5  class RunMain:
 . 6  
. 7      DEF  the __init__ (Self, URL, Method, Data = None):
 . 8          self.res = self.run_method (URL, Method, Data)
 . 9  
10  
. 11      DEF send_get (Self , URL, Data):
 12 is          "" " 
13 is          transmitted get request
 14          : param URL: request address
 15          : param Method: type (the gET)
 16          : param Data: parameters
 . 17          : return:
 18 is          " "" 
. 19         res = requests.get(url, data).json()
20         return json.dumps(res, indent=2, sort_keys=True)
21 
22     def send_post(self, url, data):
23         """
24         发送post请求
25         :param url:请求地址
26         :param method:类型(POST)
27         :param data: 参数
28         :return:
29         """
30         res = None
31         res = requests.post(url, data).json()
32         return json.dumps(res, indent=2, sort_keys=True)
33 
34     DEF run_method (Self, URL, Method, Data = None):
 35          "" " 
36          determines the type of request, and the corresponding request call
 37 [          : param URL:
 38 is          : param Method:
 39          : param Data:
 40          : return:
 41 is          " " " 
42 is          IF Method == " the GET " :
 43 is              RES = self.send_get (URL, Data)
 44 is          elif Method == " the POST " :
 45              RES = self.send_post (URL, Data)
 46 is          the else :
 47             Print ( " Write encapsulating the interface type! " )
 48          return RES
 49  
50  
51 is  IF  the __name__ == ' __main__ ' :
 52 is      URL = " http://httpbin.org/get " 
53 is      Data = {
 54 is          " username " : " test " ,
 55          " MODEL_ID " : " 12 is " 
56 is      }
 57 is  
58      RUN = RunMain (URL, "GET", data)
59 
60     print(run.res)

 

Guess you like

Origin www.cnblogs.com/CesareZhang/p/11356122.html