zoukankan      html  css  js  c++  java
  • requests模块

      1.1 session请求示例(有缓存作用,如果需要做接口关联,建议使用session请求

    from Utils.local_config_utils import  LocalConfig
    import requests
    import unittest
    import re
    class TestCases(unittest.TestCase):
    
        def setUp(self) -> None:
            self.url = LocalConfig.PHP_HOST
            self.session = requests.session()
    
        def tearDown(self) -> None:
            pass
    
    
        def test_case_regist(self):
            #请求头
            header_info = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                "Accept-Encoding": "gzip, deflate",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Content-Type":"application/x-www-form-urlencoded"
            }
            #请求参数
            get_token_param = {"m":"u",
                               "c":"register"}
    
            token_response =  self.session.get(url=self.url+'/phpwind/index.php',params = get_token_param,
                                              )
            info = token_response.content.decode('utf-8') #响应报文处理,将content返回的二进制 转换成'utf-8格式'
            token = re.findall('name="csrf_token" value="(.+?)"/></form>',info )[0]  #正则表达式提取需要的字符串
            # print(info)
            print(token)
    
            
            #post
            #请求参数
            params = {
                "m": "u",
                "c": "register",
                "a": "dorun"
            }
            #请求消息体
            data = {
                "username": "liangyq10",
                "password": "123456",
                "repassword": "123456",
                "email": "1441404602@163.com",
                "csrf_token": token
            }
            #请求
            reponse = self.session.post(url=self.url+'/phpwind/index.php',params=params,
                                    data =data,
                                    headers=header_info
                            )
            reponse.content.decode('utf-8')
    
    if __name__ == '__main__':
        testsuite = unittest.TestSuite()
        testsuite.addTest(TestCases('test_case_regist'))
        runner = unittest.TextTestRunner()
        runner.run(testsuite)

       * requests.get() 和 requests.post()与上述session请求语法一致

     

      2.1 响应:

            print("reponse.text====>",reponse.text)  #返回响应报文
            print("reponse.content====>",reponse.content)     #返回响应报文
            print("reponse.content.decode('utf-8')====>",reponse.content.decode('utf-8'))  #返回响应报文
            print("reponse.status_code====>", reponse.status_code)  #状态码
            print( "reponse.headers====>",reponse.headers)   #响应头
            print(  "reponse.json()====>",reponse.json())  #以json格式化
            print( "reponse.cookies====>",reponse.cookies)
            print( "reponse.raw====>", reponse.raw)
            print( "reponse.url====>", reponse.url)   #返回请求url和参数
            print( "reponse.apparent_encoding====>",reponse.apparent_encoding)  # ascii
            print( "reponse.history====>",reponse.history) #重定向的历史请求
            print( "reponse.raise_for_status()====>",reponse.raise_for_status()) #默认如果不是200就报异常
     
  • 相关阅读:
    工作中php处理HTTP请求的缺陷总结
    php 快速读取文件夹下文件列表
    php 对象赋值后改变成员变量影响赋值对象
    奇偶数分离小程序
    阻止安卓实体返回键后退的网页js实现
    win处navicat直接导出的sql脚本导入Linux mysql报错问题
    linux安装navicat全程记录
    【自制工具类】Java删除字符串中的元素
    【JSP/Servlet】后台如何获取复选框或可选属性的同名参数
    【Jsp/Servlet】获取客户端使用的ip
  • 原文地址:https://www.cnblogs.com/joy-field/p/13299908.html
Copyright © 2011-2022 走看看