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)

 

# 函数

a = [1, 3, 6, 4, 85, 32, 46]
print(sum(a))   # sum,求和函数

def add():
   
a = 1,
    b = 2,
    return a + b
print(add())

def add(a, b)# 都必填
    return a + b
print(add())

def add(a=0, b=0):   # 都非必填
    return a + b
print(add())

def add(a, b=0):    # a必填(必填项放前面)
    return a + b
print(add(1, 3))    # 不指定参数名,按顺序传参
print(add(a=1, b=3))    # 指定参数名,按对应参数传参

def add(a=0, b=0, *args):   # *args可变参数(参数个数可变,不限量)
    return a + b
print(add())



# def add(a, b):
#     # 理解为接口
#     return a+b
# # 入参和出参 API接口
# print(add(1, 2))

# 类和方法

class Count():

   
def __init__(self, aaa, bbb):     # 初始化
        # 可以放公共的参数
        print('实例化的时候,会执行init的内容')
       
self.a = aaa
        self.b = bbb
    # 加了self是全局变量,没加self是局部变量

    def abb(self):    # 类里面的叫方法,不叫函数
        return self.a + self.b


    def abb(self, a, b):    # 类里面的叫方法,不叫函数
        return self.a * self.b

    def aee(self, a, b):
       
# self就是对象自己,类本身的实例参数
        self.abb()  # 内部调用时直接self.方法


if __name__=='__main__':    # 下面的代码只在当前脚本运行
    count = Count()     # 把类实例化为对象,此时没有参数,实例化之后才能调用类里面的方法
    count.add(1, 3)     # 调用a对象的add方法

    b = Count(10, 5)
   
aa = b.abb()
   
print(aa)

 

# coding:utf-8
import requests

header = {   # 登录抓包获取的头部
        “User-Agent”: “Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0”,
        “Accept”: “*/*”,
        “Accept-Language”: “zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3”,
        “Accept-Encoding”: “gzip, deflate”,
        “Content-Type”: “application/x-www-form-urlencoded; charset=UTF-8”,
        “X-Requested-With”: “XMLHttpRequest”,
        “Content-Length”: “423”,
        “Connection”: “keep-alive”
         
        }

body = {“key1”: “value1”,
        “key2”: “value2”}  # 这里账号密码就是抓包的数据

s = requests.session()
login_url = “http://xxx.login”   # 自己找带token网址
login_ret = s.post(login_url, headers=header, data=body)
# 这里token在返回的json里,可以直接提取
token = login_ret.json()[“token”]   # 先取出来

# 这是登录后发的一个post请求
post_url = “http://xxx”
# 添加token到请求头
header[“token”] = token      # 不一定都在头部,仅供参考
# 如果这个post请求的头部其它参数变了,也可以直接更新
header["Content-Length"]="9"
body1 = {
         "key": "value“
          
         }
post_ret = s.post(post_url, headers=header, data=body1)
print
post_ret.content
 

猜你喜欢

转载自www.cnblogs.com/zhongyehai/p/9147756.html