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. 设计模式C++学习笔记之五(Factory Method工厂方法模式)

      工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中.核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的 ...

  2. 【转】Java并发编程:Thread类的使用

    一.线程的状态 在正式学习Thread类中的具体方法之前,我们先来了解一下线程有哪些状态,这个将会有助于对Thread类中的方法的理解. 线程从创建到最终的消亡,要经历若干个状态.一般来说,线程包括以 ...

  3. csrfguard3.1 部署笔记

    1:git clone 导入csrfguard 2:点击菜单栏View->Tool  Windows->Maven projects 3:Lifecycle clean build 4:t ...

  4. CentOS入门

    1.因修改/etc/sudoers权限导致sudo和su不能使用问题 https://blog.csdn.net/u014029448/article/details/80944380 2.给用户分配 ...

  5. HBase的replication原理及部署

    一.hbase replication原理 hbase 的复制方式是 master-push 方式,即主集群推的方式,主要是因为每个rs都有自己的WAL. 一个master集群可以复制给多个从集群,复 ...

  6. 安装mysql5.7与创建用户和远程登录授权

    环境:ubuntu18.04 参考文章:安装并远程登录授权:https://www.cnblogs.com/chancy/p/9444187.html 用户管理:https://www.cnblogs ...

  7. 洛谷P4389 付公主的背包 [生成函数,NTT]

    传送门 同样是回过头来发现不会做了,要加深一下记忆. 思路 只要听说过生成函数的人相信第一眼都可以想到生成函数. 所以我们要求 \[ ans=\prod \sum_n x^{nV}=\prod \fr ...

  8. C#操作excel(多种方法比较)

    1.用查询表的方式查询并show在数据集控件上. public static string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; D ...

  9. Idea 调试快捷键

    F9            resume programe 恢复程序 Alt+F10       show execution point 显示执行断点 F8            Step Over ...

  10. 死磕安卓前序:MVP架构探究之旅—基础篇

    前言 了解相关更多技术,可参考<我就死磕安卓了,怎么了?>,接下来谈一谈我们来学习一下MVP的基本认识. 大家对MVC的架构模式再熟悉不过.今天我们就学习一下MVP架构模式. MVC和MV ...