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. Boost property_tree解析json

    使用Boost property_tree解析json 之前使用jsoncpp解析json,现在才知道boost就有解析的库,学习一下吧 property_tree可以解析xml,json,ini,i ...

  2. Windows登录类型及安全日志解析

    Windows登录类型及安全日志解析 一.Windows登录类型 如果你留意Windows系统的安全日志,在那些事件描述中你将会发现里面的“登录类型”并非全部相同,难道除了在键盘上进行交互式登录(登录 ...

  3. HDU - 2665 Kth number 主席树/可持久化权值线段树

    题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...

  4. Android一个自定义的进度环:ProgressChart

    源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/ProgressChart.zip 因项目需要,自己尝试定义了一个进度环,用于显示进度,实现效果如 ...

  5. liux三剑客grep 正则匹配

    001正则匹配(大部分需要转义) ‘^‘: 锚定行首 '$' : 锚定行尾 [0-9] 一个数字 [^0-9] 除去数字所有,^出现在[]这里表示取反 [a-z] [A-Z] [a-Z] \s 匹配空 ...

  6. cdh5.15集群添加spark2.3服务(parcels安装)

    背景: 机器系统:redhat7.3:已经配置了http服务 集群在安装的时候没有启动spark服务,因为cdh5.15自带的spark不是2.0,所以直接进行spark2.3的安装 参考文档:htt ...

  7. STM32应用实例十一:基于SPI和AD7192的数据采集

    在开发臭氧发生器的时,我们需要一个高分辨率的AD采集,于是选择了AD7192,选择这款ADC的原因比较简单.首先它是24位的符合我们的精度要求:其次它自带时钟,便于节省空间:第三他又4路单端或2路差分 ...

  8. bat命令查询硬件信息

    bat命令查询硬件信息 50 需求是这样的写一个bat命令,当命令执行的时候,先请用户输入姓名,然后继续执行查询出以下信息并写入一个文件,文件名称随便,文件可以放在与当前命令同一个文件夹下.最终文件中 ...

  9. Confluence 6 注册单一小工具

    如果你不能订阅一个应用的小工具,你需要将小工具一个一个的添加进来.针对网站不支持小工具订阅和你的应用和你的 Confluence 不能建立信任连接的情况,你就只能这样添加了. 首先你需要获得小工具的 ...

  10. django 中的闪现

    导包 from django.contrib import messages #输出格式 messages.success(request,'不能为空') #前端页面的写法 {%if messages ...