zoukankan      html  css  js  c++  java
  • python接口自动化测试二十:函数写接口测试

    # coding:utf-8
    import requests
    import re
    from bs4 import BeautifulSoup

    # s = requests.session() # 全局的s

    def get_token(s):
    '''
    fuction: 获取token
    args: s 参数 -》s = requests.session()
    :return anti_token ->{'X-Anit-Forge-Token': 'xx', 'X-Anit-Forge-Code': '38515842'}
    '''
    # 局部的s没定义,从外部传入s
    url = 'https://passport.lagou.com/login/login.html'
    h1 = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"
    }
    r1 = s.get(url, headers=h1, verify=False)
    # print(r1.text)

    soup = BeautifulSoup(r1.content, "html.parser", from_encoding='utf-8')
    tokenCode = {}
    try:
    t = soup.find_all('script')[1].get_text()
    print(t)
    tokenCode['X_Anti_Forge_Token'] = re.findall(r"Token = '(.+?)'", t)[0]
    tokenCode['X_Anti_Forge_Code'] = re.findall(r"Code = '(.+?)'", t)[0]
    return tokenCode
    except:
    print("获取token和code失败")
    tokenCode['X_Anti_Forge_Token'] = ""
    tokenCode['X_Anti_Forge_Code'] = ""
    return tokenCode


    def login_lgw(s, anti_token, user, psw):

    url2 = 'https://passport.lagou.com/login/login.json'
    h2 = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "X-Requested-With": "XMLHttpRequest",
    "X-Anit-Forge-Token": anti_token['X_Anti_Forge_Token'] ,
    "X-Anit-Forge-Code": anti_token['X_Anti_Forge_Code'],
    "Referer": "https://passport.lagou.com/login/login.html"
    }
    body ={
    "isValidate": "true",
    "username": user,
    "password": psw,
    "request_form_verifyCode": "",
    "submit": ""
    }
    print(s.headers) # s的头部

    # 更新s的头部
    s.headers.update(h2)
    print(s.headers)
    r2 = s.post(url2, data=body, verify=False)
    print(r2.text)
    return r2.json()

    if __name__ == "__main__":
    # 自测的内容
    s = requests.session()
    token = get_token(s)
    print(token)
    user = "1232"
    psw = "322222"
    login_lgw(s, token, user, psw)


  • 相关阅读:
    java设计模式之单例模式总结
    分页功能实现
    java设计模式之代理模式模式总结
    java设计模式之策略模式总结
    快速排序解决相关问题
    单例模式之恶汉模式(详解)
    java多线程之内存的可见性介绍(备用1)
    Robotframework(4):创建变量的类型和使用
    Robotframework(3):使用pycharm编写和运行RF脚本
    Robotframework(2):创建RF第一条可执行的用例
  • 原文地址:https://www.cnblogs.com/malinalian/p/10491785.html
Copyright © 2011-2022 走看看