Interface test using python modules -Requests

Requests to use the module

Chinese Document API: http://2.python-requests.org/en/master/

1, transmits get, post request

import requests
reponse = requests.get("http://www.baidu.com")
reponse = requests.post("http://www.baidu.com")

2, the response properties

  • View response content

    • response.text property

    • respone.content.decode ( 'utf8') attribute byte format decode decoding requires

  • View response code response.status_code property

  • View Response Headers respone.headers property

  • Check cookie attribute information respone.cookies

  • View request method respone.request property

  • How to solve the need to call the login interface to log in before recharging interface to recharge?

   Method one: by creating a session object, cookie information is automatically saved last request, to resolve authentication, authorization problems.

Import Requests
 from requests.sessions Import the Session 

# create a session objects (session) 
# action: automatically saves the last request of the cookie information 
session = the Session () 

# registration interface 
register_url = " HTTP: // ip: Port / futureloan / MVC / API / Member / Register " 
register_data = { ' MobilePhone, ' : ' 18,814,726,727 ' , ' pwd ' : ' 123456 ' , ' regname ' : ' a record ' } 
register_response= session.post(url=register_url,data=register_data)
# print(register_response.status_code)
print(register_response.text)

# 登录接口
login_url = "http://ip:port/futureloan/mvc/api/member/login"
login_data = {'mobilephone':'18814726727','pwd':'123456'}
login_response = requests.get(url=login_url,data=login_data)
print(login_response.text)

# 充值接口
rech_url = "http://ip:port/futureloan/mvc/api/member/recharge"
rech_data = {'mobilephone':'18814726727','amount':'200'}
rech_response = requests.get(url=rech_url,data=rech_data)
print(rech_response.text)

The output is:

    Method two: by passing cookies in requsets request information, but also solve the authentication, authorization problems.

import requests
# 登陆接口
login_url = "http://ip:port/futureloan/mvc/api/member/login"
login_data = {'mobilephone':'18814726727','pwd':'123456'}
login_response = requests.post(url=login_url,data=login_data)
print(login_response.text)
print(login_response.cookies)
cookies = login_response.cookies

# 充值接口
rech_url = "http://ip:port/futureloan/mvc/api/member/recharge"
rech_data = {'mobilephone':'18814726727','amount':'200'}
rech_response = requests.post(url=rech_url,data=rech_data,cookies=cookies)
print(rech_response.text)

The output is:

  •  heraders application parameters

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.35 Safari/537.36",
    # "Token":"",
    "cookie":"JSESSIONID=5039A5FD7567C7C78B8180C50D340A5F",   # 请求的cookie信息
    "content-Type":"application/x-www-form-urlencoded"        # post请求格式
}
rech_response = requests.post(url=rech_url,data=rech_data,headers=headers)

3, a package requests own class

 Objective: To request method in accordance with the embodiment, to determine what types of requests initiated.

           To log output loggib

          HttpRequests class used to directly send the request information is not recorded cookies; cookies and the information can be recorded HttpRequestsCookies class.

 

Import Requests
 class HTTPRequests ():
     # directly transmits a request without recording information cookies 
    DEF Request (Self, Method, URL, the params = None, Data = None, headers = None, cookies = None, JSON = None): 
        Method = Method .lower ()
         # method of determining the request is a post or GET 
        iF method == " post " :
             # determines whether to use json post request to pass parameters (interface parameters is used to apply json parameter passing) 
            iF json:
                 # Logger .info ( "sending request, the request address: {}, request parameters: {}." the format (URL, JSON) 
                return requests.post (URL = URL, JSON = JSON, headers = headers, = Cookies Cookies)
             the else:
                 # Logger.info ( "Sending request, the request address: {}, request parameters: {}" the format (URL, Data).) 
                Return requests.post (URL = URL, Data = Data, headers = headers, Cookies = Cookies)
         elif Method == " GET " :
             # logger.info ( "sending request, the request address: {}, request parameters: {}." the format (URL, the params)) 
            return requests.get (URL = URL, the params the params =, = headers headers, cookies = cookies) 

class HttpRequestsCookies ():
     # transmission request information and the recording cookies, to the next use 
    DEF  the __init__ (Self):
         # Create a session object 
        self.session = requests.sessions.Session ()
    DEF Request (Self, Method, URL, the params = None, Data = None, headers = None, Cookies = None, JSON = None): 
        Method = method.lower ()
         # method of determining the request is a post or GET 
        IF Method == " post " :
             # determines whether to use json post request to pass arguments (parameters is applied to the interface used json parameter passing) 
            iF json:
                 # logger.info ( "sending request, the request address: {}, request parameters: } { "the format (URL, Data)). 
                return self.session.post (URL = URL, JSON = JSON, headers = headers, = Cookies Cookies)
             the else :
                 # logger.info (" sending request, the request address: { } request parameters:. {} "the format (URL, Data)) 
                returnself.session.post (URL = URL, Data = Data, headers = headers, = Cookies Cookies)
         elif Method == " GET " :
             # logger.info ( "Sending request, the request address: {}, request parameters: { .} "the format (URL, the params)) 
            return self.session.get (URL = URL, the params = the params, headers = headers, = Cookies Cookies)
     # disconnected session connection 
    DEF Close (Self): 
        self.session.close ( ) 
  
IF  __name__ == " __main__ " : 
    r = HttpRequestsCookies ()
     # login interfaces 
    LOGIN_URL = "http://ip:port/futureloan/mvc/api/member/login"
    login_data = {'mobilephone': '18814726727', 'pwd': '123456'}
    response = r.request('post',url=login_url, data=login_data)
    print(response.text)
    # 充值接口
    rech_url = "http://ip:port/futureloan/mvc/api/member/recharge"
    rech_data = {'mobilephone': '18814726727', 'amount': '200'}
    response = r.request('post', url=rech_url, data=rech_data)
    print(response.text)

 

 

The output is:

 

Guess you like

Origin www.cnblogs.com/wanglle/p/11581677.html