request simple application

Brief introduction

requests that third-party packages Python, used in Python to apply for a request to see the return value, but also to make automated request very helpful

Download method

pip install requests

 

Instructions

  • Get a simple example

 

"""
    request
"""
# Import third-party packages
import requests

def test_01():
    res = requests.get ( "https://cn.bing.com/") # address acquisition request
    print (res.text) # print data is returned
    Header print (res.headers) # Response
    print (res.status_code) # http response status code
    print (res.cookies) # cookie display
if __name__ == "__main__":
    test_01()

 

  • A simple example of a post

School post request, there are three methods of work of the commonly used parameters are passed, it is in accordance with the requirements document to

  • form-data
  • x-www-form-urlencoded
  • raw-json

The first step, we enter to create a post request postman and run

 

 The second step, click on the code

 

 The third step, select Requests, paste the code to be modified in Python

 

The fourth step to view the information 

"""
    request
"""
# Import third-party packages
import requests

def test_02_post_formdata():
    u = "http://132.232.44.158:8080/morning/user/userLogin" # interface address
    Parameter # payload, formdata format
    
    d = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"user.loginName\"\r\n\r\[email protected]\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"user.loginPassword\"\r\n\r\na123456\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
    r = requests.post (url = u, data = d) # post method to open by the url
    # Here is the information returned
    
    print(r.text)
    print(r.status_code)
    The results shown here are empty #

if __name__ == "__main__":
    test_02_post_formdata()
"""
    request
"""
# Import third-party packages
import requests

def test_post_json():
"""
  Json file in the mass participation, json is a dictionary of key-value pairs in the form of mass participation
"""
    u = "http://132.232.44.158:5000/userLogin/"
    d = {"username":"test", "password":"123456", "captcha":"123456"}
    r = requests.post(url = u,json = d)
    print(r.text)


if __name__ == "__main__":
    test_post_json()

Note: If a single interface, you do not add headers, if it is multi-interface, it will involve cookie, then finally add headers, avoid unnecessary mistakes

 

request the testing process

step1: We tested a login process, where we have to look at the database, so the database associated with the first, first create a utils to write code before placing pymysql, which put a __init__.py file, so there pymysql the method can be cited

 pymysql Code

import pymysql

def init(host,user,password,db):
    db = pymysql.connect(host,user,password,db)
    return db

"""
    Query operation
"""
def query(sql,db):
    """
        Create a method to query the database of
    """
    cursor = db.cursor () # get a cursor window
    try:
        cursor.execute (sql) # sql statement execution
        res = cursor.fetchall () # Get Return value
        db.close () # close the database
        return res
    except:
        print ( "sql statement error")
        return False

def commit(sql,db):
    """
        The table to add, delete, modify can
    """
    cursor = db.cursor()
    try:
        cursor.execute (sql) # sql statement execution
        db.commit () # save the data
        return True
    except:
        print ( "sql statement error")
        return False

request Code

"""
    Test Procedure
"""
import requests
from utils import demo02

# Step1, configured to request
u = "http://132.232.44.158:5000/userLogin/"
d = {"username":"test", "password":"123456", "captcha":"123456"}
r = requests.post(url = u,json = d)
print(r.text)

# Step2, asserted http response status code, namely non-life death 
# This is the entire Internet to develop rules, response code assert r.status_code == 200 # Step3, asserted response information res = r.json () # res into a dictionary, provided that the information returned by the server must be json format assert res.get ( "code") == 200 # This is the company's internal rules established # Step4, query the database to see if this record (optional, login, order ....) # Purpose: To determine whether there is a problem code behind sql = "select * from tbl_user where username='test' and password='123456'" res = demo02.init("132.232.44.158","vn","Langjintest!@#4##","lux") reesult = demo02.query(sql,res) assert len(reesult) == 1 print ( "test case by login")

 

Guess you like

Origin www.cnblogs.com/cheneyboon/p/11868998.html