zoukankan      html  css  js  c++  java
  • Python接口测试学习笔记(三)

    本文学习笔记整理自【Python开发】接口测试教程

    一. Requests简介

    Requests是一个Python第三方库,处理URL资源特别方便

    cmd输入: pip install requests (pip不能用, 可去python安装目录的scripts里的地址栏输入cmd后使用)

    Pycharm不能导入通过pip安装的模块, 建议直接通过方式安装requests模块:

    File --> settings... --> Project:xx --> Project Interpreter --> 点击+ 号 --> 搜索模块名 --> install package

    Note:

    1. pip list可查看当前安装的模块

    2. Requests官方中文文档:http://2.python-requests.org/zh_CN/latest/index.html

    二. get

    1. 发get请求

    用get方法就能直接访问url地址

    get方法的参数, 第一个为请求的url, 第二个为请求的参数, 如果参数没填即默认为空

    -- status_code方法查看状态码

    -- text是返回文本信息 

    1 # coding: utf-8  # 中文编码声明
    2 
    3 import requests
    4 
    5 # 发送get请求并返回请求的response
    6 res = requests.get("https://home.cnblogs.com/u/fayez/")
    7 
    8 print(res.status_code)
    9 print(res.text)

    2. Get带参数params

    (以: https://cn.bing.com/search?q=test 为例)

    字典形式传参:{"key1":"value1","key2":"value2"}

     1 # coding: utf-8  # 中文编码声明
     2 
     3 import requests
     4 
     5 # https 禁warning
     6 requests.packages.urllib3.disable_warnings()
     7 
     8 url = "https://cn.bing.com/search"
     9 
    10 """
    11 参数的参数化:
    12 searchKeywords="python"
    13 parm = {"q": "%s"%searchKeywords}
    14 """
    15 # 参数存字典
    16 parm = {"q": "test"}
    17 
    18 # url和参数一一对应, https需要加verify
    19 res = requests.get(url=url, params=parm, verify=False)
    20 
    21 # res.url获取当前url地址, 可以检查是否传参成功
    22 print(res.url)
    23 print(res.text)

    3. 定制请求头

    有些网站防爬虫, 没有头部容易被服务器拒绝

    手动访问, 抓取头部, 以字典形式放到代码中, 传参headers

    1 # header可加其他参数
    2 header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}
    3 res = requests.get(url=url, params=parm, headers=header, verify=False)

    4. Gzip解码

    有些响应内容是gzip压缩的, text只能打印文本内容, 用content是二进制  --- 一般获取返回值内容, 推荐用content

    1 import requests
    2 
    3 r = requests.get("https://www.baidu.com/")
    4 print(r.encoding)
    5 # 正文用二进制流输出
    6 print(r.content)
    7 # utg-8格式的字符串
    8 print(r.text)

    5. Response返回内容

    res.status_code  响应状态码                                                                                                                         
    res.content 字节方式的响应体, 会自动解码gzip和deflate压缩  
    res.headers 以字典对象存储服务器响应头, 字典键不区分大小写, 若键不存在则返回None  
    res.json() Requests中内置的JSON解码器  
    res.url 获取url  
    res.encoding 编码格式  
    res.cookies 获取cookie  
    res.raw 返回原始响应体  
    res.text 字符串方式的响应体, 会自动根据响应头部的字符编码进行解码  
    res.raise_for_status() 失败请求(非200响应)抛出异常  

                

    三. post

    1. https请求(ssl证书)

    https的请求相对于http安全级别高, 需要验证ssl证书, Requests可以为HTTPS请求严重SSL证书, 要想检查某个主机的SSL证书, 你可以使用verify参数:

    如果将verify设置为false, requests也能忽略对SSL证书的验证. 禁用warning: requests.packages.urllib3.disable_warnings()

    2. post请求参数:

    1. post请求参数一部分在url里, 另外一部分在body里面

    2. post请求体的参数格式(Content-Type):

    1. application/json: {"input1":"xxx","input2":"xxx"}

    2. application/x-www-form-urlencoded: input1=xxx&input2=xxx

    3. multipart/form-data: 表单格式(文件上传, 图片上传等混合式)

    4. text/xml

    3. 发送body(data/json)

    1. application/json格式的post请求:

    payload={"input1":"xxx","input2":"xxx"}
    res = requests.post(url=url,params=parm,json=payload,headers=header,verify=False)

    2. application/x-www-form-urlencoded格式的post请求

    传data参数即可无需转json(也可内嵌json)

    payload={"input1":"xx","input2":"xx","json":{"input1":"xxx","input2":"xxx"}}
    res = requests.post(url=url,data=payload,headers=header,verify=False)

    Note: post方法参数里面一个传的是json, 一个传的是data

    4. data 和 json的区分

    1. 抓包看头部Content-type参数:

    application/json --> json

    application/x-www-form-urlencoded --> data

    2. 请求体的参数长相:

    {"input1":"xxx","input2":"xxx"} --> json

    name1=value1&name2=value2 --> data

  • 相关阅读:
    GDAL指定自定义的金字塔目录
    同一个脚本在SQLPLUS和SQLDEV上的不同
    【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
    unity3d的playmaker插件使用教程,三、对象出入触发,声音播放
    MapReduce实现矩阵乘法
    Nginx+Tomcat搭建高性能负载均衡集群
    怎么去掉Xcodeproject中的某种类型的警告 Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int32
    <html>
    实战c++中的vector系列--vector&lt;unique_ptr&lt;&gt;&gt;初始化(全部权转移)
    [Mac] mac linux 多线程下载利器 axel
  • 原文地址:https://www.cnblogs.com/fayez/p/12234243.html
Copyright © 2011-2022 走看看