请求设置:requests.get/post

            (

            url,

            data={},

            params={},

            headers={},

            timeout=0.01,

            files={}

             Session() 

                                       verity=False  关闭ssl验证

                                    proxies={}   代理设置
                                    auth=HTTPBasicAuth() base认证
            auth=HTTPDigestAuth()  digest认证
 

            )

响应获取:response .status_code 

          url 

          cookies

          text

          content    用来保存图片,视频

          json()

 

1.说明

data={}  用于get请求

params={} 用于post请求

2.session()用法示例

 场景一:

response = requests.get('http://httpbin.org/cookies/set/name/tom')
print(response.text) response2 = requests.get('http://httpbin.org/cookies')
print(response2.text) 结果:{"cookies": {"name": "tom"}} {"cookies": {}} 获取不到cookie 场景二:
obj = requests.session()
rec = obj.get('http://httpbin.org/cookies/set/name/tom')
print(rec.text) rec2 = obj.get('http://httpbin.org/cookies')
print(rec2.text)

结果:{"cookies": {"name": "tom"}}             {"cookies": {"name": "tom"}} 获取到cookie
 

3.verity用法示例

response = requests.get('https://www.12306.cn',verify=False) 如果不关闭verity会报错
print(response.text.encode("utf8"))

4.代理设置用法

proxies = {'http' : '61.135.217.7:80'}
response = requests.get('http://httpbin.org/get', proxies=proxies)
print(response.text)

5.auth用法示例

import requests
from requests.auth import HTTPBasicAuth
from requests.auth import HTTPDigestAuth response = requests.get('http://httpbin.org/basic-auth/mother/home', auth=HTTPBasicAuth("mother","home"))
print(response.text)
response2 = requests.get('http://httpbin.org/digest-auth/auth/my_mother/home', auth=HTTPDigestAuth("my_mother","home"))
print(response2.text)

5.模拟上传图片

file = {'img':open('df.jpg','rb')}
response = requests.post('http://httpbin.org/post',files=file)
print(response.text)

 6.字典形式的url拼接

from urllib.parse import urlencode
data = {'city':'北京'}
print("https://www.sojson.com/?%s" % urlencode(data))

 7.例子:unittest + requests + BSTestRunner

  BSTestRunner :形成测试报告的库

1.安装 BSTestRunner 

地址:https://github.com/easonhan007/HTMLTestRunner

将 BSTestRunner.py 文件放入目录 D:\IT\python3\Lib 下

2.开始工程(用例+测试报告)



import unittest
import time
from BSTestRunner import BSTestRunner test_dir = './test_cases'
report_dir = './reports'
discover = unittest.defaultTestLoader.discover(test_dir , pattern="dan.py")
now = time.strftime('%Y-%m-%d %H_%M_%S')
report_name = report_dir+'/'+now+'test_report.html' with open(report_name,'wb') as f:
runner = BSTestRunner(stream=f,title='Weather API Test Report',description='china wether')
runner.run(discover)

requests 常见方法总结的更多相关文章

  1. Python爬虫突破封禁的6种常见方法

    转 Python爬虫突破封禁的6种常见方法 2016年08月17日 22:36:59 阅读数:37936 在互联网上进行自动数据采集(抓取)这件事和互联网存在的时间差不多一样长.今天大众好像更倾向于用 ...

  2. C#图片处理常见方法性能比较

    C#图片处理常见方法性能比较 来自:http://www.cnblogs.com/sndnnlfhvk/archive/2012/02/27/2370643.html   在.NET编程中,由于GDI ...

  3. window对象中的常见方法

    <body><!-- window对象中的常见方法--><script type="text/javascript"> var timeid; ...

  4. python socket 常见方法及 简单服务/客户端

    socket 常见方法: 补充说明:what is file descriptor? 文件描述符是什么? 参考(http://stackoverflow.com/questions/8191905/w ...

  5. VBS操作Excel常见方法

    VBS操作Excel常见方法 作者: 字体:[增加 减小] 类型:转载 时间:2009-11-13我要评论 VBS控制Excel常见方法,需要的朋友可以参考下. dim oExcel,oWb,oShe ...

  6. UIPickerView常见属性、常见方法(包括代理方法和数据源方法)的一些说明

    一.UIPickerView 1.UIPickerView的常见属性 // 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id< ...

  7. jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...

  8. AJAX跨域的常见方法

    由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问.比如说你的网站域名是aaa.com,想要通过AJAX请求 ...

  9. Java中字符串的一些常见方法

    1.Java中字符串的一些常见方法 /** * */ package com.you.model; /** * @author Administrator * @date 2014-02-24 */ ...

随机推荐

  1. JEE Spring-boot 简单的ioc写法。

    什么是ioc,就是你可能会有一些生活必需品,这些东西你必须要用才能存活.但是你不是每天都回去买,去哪一家点去买.而这些用品会一直放在哪里,每一个商店就是一个容器,包裹着这些物品. 创建ioc项目,首先 ...

  2. SpringMVC视频

    视频内容: 1.下载spring mvc以及spring mvc示例演示http://pan.baidu.com/s/1kTHRfDH 2.配置完善&初步探究控制器拦截http://pan.b ...

  3. DataTables入门

    转载 https://blog.csdn.net/gfd54gd5f46/article/details/65938189

  4. node——module.exports

    module.exports 1. 在a.js中 var b=require('./b.js'); console.log(b); 在b.js中 function add(x,y){ return x ...

  5. HDU 6149 Valley Numer II (状压DP 易错题)

    题目大意:给你一个无向连通图(n<=30),点分为高点和低点,高点数量<=15,如果两个高点和低点都直接连边,那么我们称这三个点形成一个valley,每个点最多作为一个valley的组成部 ...

  6. NOI 2011 阿狸的打字机 (AC自动机+dfs序+树状数组)

    题目大意:略(太长了不好描述) 良心LOJ传送门 先对所有被打印的字符串建一颗Trie树 观察数据范围,并不能每次打印都从头到尾暴力建树,而是每遍历到一个字符就在Trie上插入这个字符,然后记录每次打 ...

  7. Vue基础操作

    一.Vue入门基础知识 1.Vue使用的基本操作 i. 先下载,引入vue.jsii. Vue,实例化一个vue实例化对象(new Vue({})) 1. 新建一个vue实例化对象(Vue是一个构造函 ...

  8. [terry笔记]学校管理系统

    如下是要求: # 角色:学校.学员.课程.讲师# 要求:# 1. 创建北京.上海 2 所学校# 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上 ...

  9. 在Windows系统下搭建Redis集群

    准备工作 需要4个部件:Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb.使用redis-trib.rb工具来 ...

  10. iOS UI16_数据持久化

    // // Student.h // UI16_数据持久化 // // Created by dllo on 15/8/19. // Copyright (c) 2015年 zhozhicheng. ...