Problem Description: Some system interfaces verify whether a user has logged in to the system by verifying the token value sent after the login interface succeeds. That is, when requesting all system interfaces, the front-end interface must carry the token returned after the login, and the background checks whether the user has logged in or expired. All interfaces depend on the token after successful login. You can associate the token. This case implements the request login interface, obtains the token, and associates the token with another GET or POST request

Implementation steps:

import json
import requests
import urllib.parse
import urllib.request

def doLogin():
    url = "https://... /manage/novel/doLogin"
    Request based on the parameters and methods required by the login interface
    values = {'account':'* * *'.'pwd':'* * *'.'v_token':'* * *'.'verify':'* * *'}
    values_json = json.dumps(values)
    # Login returns token
    req = requests.post(url, data=values_json)
    token = req.json()["rel"] ["token"]
    return token

# Associate the token returned by doLogin() to perform a GET or POST request, in this case a GET request
def getBaselist():
    url = "https://... /manage/login/list"
    values = {}
    values['token'] = doLogin()
    data = urllib.parse.urlencode(values)
    req = url + '? ' + data
    response = urllib.request.urlopen(req)
    the_page = response.read()
    print(the_page.decode("unicode_escape"))

getBaselist()

Copy the code
  • Req.json ()[“rel”][“token”]

  • Result: Get request data is returned