1.预配置

import requests
ss = requests.Session()
ss.headers.update({'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'}) # C:\Program Files\Anaconda2\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning:
# Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: ht
# tps://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
ss.verify = False
from urllib3.exceptions import InsecureRequestWarning
from warnings import filterwarnings
filterwarnings('ignore', category = InsecureRequestWarning) # http://docs.python-requests.org/zh_CN/latest/api.html
import logging
try:
from http.client import HTTPConnection
except ImportError:
from httplib import HTTPConnection
HTTPConnection.debuglevel = 0 #关闭 0 开启 1 logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True def s_hooks(r, *args, **kwargs):
print('#'*20)
print(r.url, r.request.method, r.status_code, r.reason)
print('request Content-Type: {} body: {}'.format(r.request.headers.get('Content-Type'), r.request.body))
print('response Content-Type: {} body length: {}'.format(r.headers.get('Content-Type'), len(r.content)))
if 'json' in r.headers.get('Content-Type', '').lower():
print(r.content)
print('#'*20)
ss.hooks = dict(response=s_hooks) def httpbin(postfix=''):
return 'http://httpbin.org/' + postfix # r = ss.post(httpbin('post'),json=dict([('a',1),('b',2)]) )

2.Content-Type

Sending JSON in Requests

import requests
from pprint import pprint
ss = requests.Session()
ss.headers['Content-Type'] = 'application/myjson'
r = ss.post('https://httpbin.org/post', json={'my': 'json'}, headers={'Content-Type':'somejson'})
pprint(r.json())

优先级由高到低: 传参 headers={'Content-Type':'somejson'}  >>> ss.headers >>> 传参 json= 自动生成的 u'Content-Type': u'application/json'

3. Using Retries in requests

Retries in Requests

(1)简单粗暴

s.mount('https://', HTTPAdapter(max_retries=5))

(2)更细粒度

from requests.packages.urllib3.util import Retry
from requests.adapters import HTTPAdapter
from requests import Session s = Session()
s.mount('https://pypi.python.org/', HTTPAdapter(
max_retries=Retry(total=5, status_forcelist=[500, 503])
)
)
r = s.get('https://pypi.python.org/simple/requests/')

python之requests 乱七八糟的更多相关文章

  1. Python爬虫之使用Fiddler+Postman+Python的requests模块爬取各国国旗

    介绍   本篇博客将会介绍一个Python爬虫,用来爬取各个国家的国旗,主要的目标是为了展示如何在Python的requests模块中使用POST方法来爬取网页内容.   为了知道POST方法所需要传 ...

  2. Python——安装requests第三方库

    使用pip安装 在cmd下cd到这个目录下C:\Python27\Scripts,然后执行pip install requests 在cmd 命令行执行 E:   进入e盘 cd  Python\pr ...

  3. 关于Python ,requests的小技巧

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xie_0723/article/details/52790786 关于 Python Request ...

  4. Python之Requests的高级用法

    # 高级用法 本篇文档涵盖了Requests的一些更加高级的特性. ## 会话对象 会话对象让你能够跨请求保持某些参数.它也会在同一个Session实例发出的所有请求之间保持cookies. 会话对象 ...

  5. python的requests快速上手、高级用法和身份认证

    https://blog.csdn.net/qq_25134989/article/details/78800209 快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其 ...

  6. Python 安装requests和MySQLdb

    Python 安装requests和MySQLdb 2017年10月02日 0.系统版本 0.1 查看系统版本 [root@localhost ~]# uname -a Linux localhost ...

  7. 【转】使用Python的Requests库进行web接口测试

    原文地址:使用Python的Requests库进行web接口测试 1.Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写, ...

  8. Python+Unittest+Requests+PyMysql+HTMLReport 接口自动化框架

    整体框架使用的是:Python+Unittest+Requests+PyMysql+HTMLReport  多线程并发模式 主要依赖模块 Unittest.Requests.PyMysql.HTMLR ...

  9. 对比3种接口测试的工具:jmeter+ant;postman;python的requests+unittest或requests+excel

    这篇随笔主要是对比下笔者接触过的3种接口测试工具,从实际使用的角度来分析下3种工具各自的特点 分别为:jmeter.postman.python的requests+unittest或requests+ ...

随机推荐

  1. 【转】Java的接口和抽象类

    对于面向对象编程来说,抽象是它的一大特征.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有很多相似的地方,又有很多不同的地方. 一.抽象类 在了解抽象类之前,先来了解一下抽象方 ...

  2. Select2日常操作集合

    1.获得多选值 var arraySelected = $('#carTypes').select2("data"); var carTypesDesc = ''; for (va ...

  3. java8 常用函数式接口

    public static void main(String[] args) { // TODO Auto-generated method stub //函数式接口 Function<Inte ...

  4. Jquery简单使用

    展示:$("#id").show(); 隐藏:$("#id").hide();

  5. CSS 三角形与圆形

    1. 概述 1.1 说明 通过边框(border)的宽度与边框圆角(border-radius)来设置所需的三角形与圆形. 1.2 边框 宽高都为0时,边框设置的不同结果也不同,如下: 1.四个边框都 ...

  6. 牛客网 python 求解立方根

    •计算一个数字的立方根,不使用库函数 详细描述: •接口说明 原型: public static double getCubeRoot(double input) 输入:double 待求解参数 返回 ...

  7. 【MySql】Group By数据分组

    GROUP BY 语句根据一个或多个列对结果集进行分组. 在分组的列上我们可以使用 COUNT, SUM, AVG,等函数. 因为聚合函数通过作用于一组数据而只返回一个单个值, 因此,在SELECT语 ...

  8. Confluence 6 已经存在的安装配置数据库字符集编码

    针对已经存在的 Confluence 安装,如果你安装的 Confluence 版本是 6.4 或者早期的版本,我们在安装的时候没有检查你数据库的字符设置. 如果你的数据库当前没有被配置使用 UTF- ...

  9. Confluence 6 数据库支持的驱动

    数据库 驱动已捆绑? JDBC 驱动 备注 更多信息 PostgreSQL 9.4-1202 JDBC 41 driver download 我们推荐你使用 JDBC 4 的驱动. 如果你希望使用更新 ...

  10. 关于vue的基础概念

    vue-cli相当于脚手架 给你自动生成模板工程vue-router是 vue路由插件 支持你单页应用的vue-loader是webpack下loader插件 可以把.vue文件 输出成组件