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. genPanel.py

    #!/usr/bin/python # -*- coding: UTF-8 -*- import os import sys import re import shutil import glob ' ...

  2. c# partial 分部类和分部方法

    一.partial 它是一个关键字修饰符.可以将类或结构.接口或方法的定义拆分到两个或更多个源文件中. 每个源文件包含类型或方法定义的一部分,编译应用程序时将把所有部分组合起来.修饰符不可用于委托或枚 ...

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

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

  4. malloc(0)

    malloc的内存分配之 malloc(0)的内存分配情况 #include<iostream> using namespace std; int main() { char *p; if ...

  5. ASP.NET MVC5高级编程 之 路由

    每个ASP.NET MVC应用程序都需要路由来定义自己处理请求的方式.路由是MVC应用程序的入口点.路由的核心工作是将一个请求映射到一个操作 路由主要有两种用途: 匹配传入的请求(该请求不匹配服务器文 ...

  6. python 基础 01

    什么是计算机? cpu: 计算机的大脑; 读写速度 3GHZ 内存: (为了提高利用率) 缓冲硬盘和cpu 硬盘: 机械硬盘读写速度70mb/s 计算机里面读写的内容都是01代码 二进制(计算机只认二 ...

  7. <转载>ford-fulkerson算法

    原文链接http://blog.csdn.net/ivan_zgj/article/details/51580993 最大流问题常常出现在物流配送中,可以规约为以下的图问题.最大流问题中,图中两个顶点 ...

  8. 进程、线程、GIL、同步、异步、并行、并发、互斥锁

  9. Codeforces 1132G Greedy Subsequences [线段树]

    洛谷 Codeforces 看到题解那么少就来发一篇吧-- 思路 看完题目一脸懵逼,感觉无从下手. 莫名其妙地想到笛卡尔树,但笛卡尔树好像并没有太大作用. 考虑把笛卡尔树改一下:每个点的父亲设为它的右 ...

  10. 通过cmd 使用 InstallUtil.exe 命令 操作 windows服务 Windows Service

    要安装windows service 首先要找到 InstallUtil.exe,InstallUtil.exe位置在 C:\Windows\Microsoft.NET\Framework\v4.0. ...