Python requests介绍之接口介绍
Python requests介绍
引用官网介绍
Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用。
Requests 允许你发送纯天然,植物饲养的 HTTP/1.1 请求,无需手工劳动。你不需要手动为 URL 添加查询字串,也不需要对 POST 数据进行表单编码。Keep-alive 和 HTTP 连接池的功能是 100% 自动化的,一切动力都来自于根植在 Requests 内部的 urllib3。
功能特性
Requests 完全满足今日 web 的需求。
- Keep-Alive & 连接池
- 国际化域名和 URL
- 带持久 Cookie 的会话
- 浏览器式的 SSL 认证
- 自动内容解码
- 基本/摘要式的身份认证
- 优雅的 key/value Cookie
- 自动解压
- Unicode 响应体
- HTTP(S) 代理支持
- 文件分块上传
- 流下载
- 连接超时
- 分块请求
- 支持
.netrc
Requests 支持 Python 2.6—2.7以及3.3—3.7,而且能在 PyPy 下完美运行。
''' requests 接口介绍 '''
#-*-coding:utf-8-*-
# Time:2017/10/12 22:54
# Author:YangYangJun #开始 requests学习之旅,这里直接在代码编写文字了。 #requests 是python的第三方库,所以这里需要安装 # 直接在命令行窗口执行 pip install requests 即可 #如果提示安装失败,可以参看提示,安装必备软件然后重新安装即可 #安装成功后 即可直接导入 import requests #如上不报错即可 #参考资料 #访问网址 http://cn.python-requests.org/zh_CN/latest/ 即可查看官方中文文档 #或是查看源代码 https://github.com/requests/requests #也可到 自己的安装目录下查找 D:/SProgram/Python/Lib/site-packages/requests/api.py 等文件查看介绍 ''' requests 接口介绍 '''
# 这部分文档包含了 Requests 所有的接口。对于 Requests 依赖的外部库部分,我们在这里介绍最重要的部分,并提供了规范文档的链接。
#
# 主要接口
# Requests 所有的功能都可以通过以下 7 个方法访问。它们全部都会返回一个 Response 对象的实例。 # 1、request()方法
# requests.request(method, url, **kwargs) Constructs and sends a Request. 构造和发送请求。
# 这里的参数 method 、 url 是必选参数,**kwargs 是可选参数,是 可变关键字参数,Dictionary 类型
'''
参数:
method -- method for the new Request object.
新请求对象的方法
url -- URL for the new Request object.
新的请求对象的URL
params -- (optional) Dictionary or bytes to be sent in the query string for the Request.
(可选)在查询字符串中为请求发送的字典或字节。
data -- (optional) Dictionary or list of tuples [(key, value)] (will be form-encoded), bytes, or file-like object to send in the body of the Request.
(可选)字典或元组列表[(键,值)](将以表单编码)、字节或类似文件的对象发送到请求体中。
json -- (optional) json data to send in the body of the Request.
(可选)json数据发送到请求体中。
headers -- (optional) Dictionary of HTTP Headers to send with the Request.
(可选)HTTP报头字典,以发送请求。
cookies -- (optional) Dict or CookieJar object to send with the Request.
(可选)命令或CookieJar对象发送请求。
files -- (optional) Dictionary of 'name': file-like-objects (or {'name': file-tuple}) for multipart encoding upload. file-tuple can be a 2-tuple
('filename', fileobj), 3-tuple ('filename', fileobj, 'content_type') or a 4-tuple ('filename', fileobj, 'content_type', custom_headers),
where 'content-type' is a string defining the content type of the given file and custom_headers a dict-like object containing additional
headers to add for the file.
438/5000
(可选)“名称”的字典:文件- like- object(或{' name ':file - tuple}),用于多部分编码上传。文件- tuple可以是一个2元组(文件名,fileobj),
3元组(文件名,fileobj,' content_type ')或一个4元组(' filename ',fileobj,' content_type ',custom_headers),
其中' content-type '是一个字符串,它定义了给定文件的内容类型和custom_headers,其中包含了一个类似于dict的对象,其中包含了添加文件的附加头文件。 auth -- (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
(可选)Auth tuple来启用Basic /Digest /自定义HTTP Auth。
timeout (float or tuple) -- (optional) How many seconds to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
(可选)在放弃之前,等待服务器发送数据的时间是多少秒,作为一个浮点数,或者是一个(连接超时,读超时)元组。
allow_redirects (bool) -- (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to True.
(可选)布尔。启用/禁用GET /选项/ POST / PUT /补丁/删除/头重定向。默认值为True。
proxies -- (optional) Dictionary mapping protocol to the URL of the proxy.
(可选)字典映射协议到代理的URL。
verify -- (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to True.
(可选)一个布尔值,在这种情况下,它控制我们是否验证服务器的TLS证书,或者是一个字符串,在这种情况下,它必须是一个到CA包使用的路径。默认值为True。
stream -- (optional) if False, the response content will be immediately downloaded.
(可选)如果错误,将立即下载响应内容。
cert -- (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
(可选)如果字符串,路径到ssl客户机cert文件(. pem)。如果是Tuple(' cert ',' key ')对。
''' # request()方法实例
req = requests.request('GET', 'http://httpbin.org/get') print req
# 得到 <Response [200]> # 2、head()方法 # requests.head(url, **kwargs)
# Sends a HEAD request. 发送一个HEAD 请求。 # Optional arguments that ``request`` takes. 请求获取的可选参数。
r"""Sends a HEAD request. :param url: URL for the new :class:`Request` object.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
# head()方法实例
req = requests.head('http://httpbin.org/get') print req
# 得到 <Response [200]> # 3、get()方法 # requests.get(url, params=None, **kwargs)[源代码]
# Sends a GET request.
#
# 参数:
# url -- URL for the new Request object.
# params -- (optional) Dictionary or bytes to be sent in the query string for the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response # get()方法实例
req = requests.get('http://httpbin.org/get') print req
# 得到 <Response [200]> # 4、post()方法 # requests.post(url, data=None, json=None, **kwargs)[源代码]
# Sends a POST request.
#
# 参数:
# url -- URL for the new Request object.
# data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request.
# json -- (optional) json data to send in the body of the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response # post()方法实例
req = requests.post('http://httpbin.org/get') print req
# 得到 <Response [405]> # 5、put()方法 # requests.put(url, data=None, **kwargs)[源代码]
# Sends a PUT request.
#
# 参数:
# url -- URL for the new Request object.
# data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request.
# json -- (optional) json data to send in the body of the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response # 6、patch()方法 # requests.patch(url, data=None, **kwargs)[源代码]
# Sends a PATCH request.
#
# 参数:
# url -- URL for the new Request object.
# data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request.
# json -- (optional) json data to send in the body of the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response # 7、delete()方法 # requests.delete(url, **kwargs)[源代码]
# Sends a DELETE request.
#
# 参数:
# url -- URL for the new Request object.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response
#
Python requests介绍之接口介绍的更多相关文章
- 转载:python + requests实现的接口自动化框架详细教程
转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...
- 使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)
一直听说python requests库对于接口自动化测试特别合适,但由于自身代码基础薄弱,一直没有实践: 这次赶上公司项目需要,同事小伙伴们一起学习写接口自动化脚本,听起来特别给力,赶紧实践一把: ...
- python + requests实现的接口自动化框架详细教程
前段时间由于公司测试方向的转型,由原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测试,后来,组内有人讲原先web自动化的测试框架移驾成接口的自 ...
- python Requests模块的简要介绍
Requests的安装: pip install Requests Requests的使用: import requests url = "http://www.mzitu.com" ...
- 使用python+requests+unittest实现接口自动化测试
这两天一直在找直接用python做接口自动化的方法,在网上也搜了一些博客参考,今天自己动手试了一下. 一.整体结构 上图是项目的目录结构,下面主要介绍下每个目录的作用. Common:公共方法:主要放 ...
- 基于Python + requests 的web接口自动化测试框架
之前采用JMeter进行接口测试,每次给带新人进行培训比较麻烦,干脆用python实现,将代码和用例分离,易于维护. 项目背景 公司的软件采用B/S架构,进行数据存储.分析.管理 工具选择 pytho ...
- python+requests库,接口自动化
1.requests库的使用 requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: “ ...
- python+requests+yaml实现接口自动化用例
前言:最近也思考了一下怎么做接口自动化,以下内容属于自己目前阶段所学习到的内容,也逐渐投入自己实际工作中,把最近的学习新得跟大家分享下,话不多说,切入正题. 对接口自动化测试用例的思考:接口测试大多测 ...
- python+requests+yaml实现接口自动化用例(二)---升级版
一.前言:前面一段时间封装的接口自动化测试框架用了一段时间发现还是有很多弊端的,目前又改良了一下,可以说整体思路全都推翻了,功能比之前强大许多,有兴趣的可以私信我单独交流,希望共同学习进步! 二.项目 ...
随机推荐
- UVA10066
/* 最长公共子序列 */ #include <cstdio> #include <string.h> #include <iostream> const int ...
- BabelMap 12.0.0.1 汉化版(2019年3月11日更新)
软件简介 BabelMap 是一个免费的字体映射表工具,可辅助使用<汉字速查>程序. 该软件可使用系统上安装的所有字体浏览 Unicode 中的十万个字符,还带有拼音及部首检字法,适合文献 ...
- 奋斗史-IT女生是怎样炼成的
IT女生的奋斗史 终于来到了毕业季,感觉有点伤感! 记得11年来到大学的时候,还是那么懵懂,什么都不懂,几乎连电脑的基本操作都不会!自己小时候虽然家里穷,但是从小就觉得“软件”是个很神奇的东西,很了不 ...
- Ignite内存数据库与sql支持
Ignite采用h2作为内存数据库,支持h2的一切sql语法.如果是本地缓存或者复制缓存,sql执行直接在本地h2数据库中执行,如果是分区缓存,ignite则会分解sql到多个h2数据库执行后再汇总. ...
- 简单理解offsetleft、offsetTop、offsetParent
先来看看offsetParent返回的是什么值 ele.offsetParent返回的是ele元素最近的并且是定位过(relative,absolute)的父元素,如果没有父元素或者是父元素中没有一个 ...
- Linux基础命令---zcat
zcat 解压有gzip压缩的文件,将解压结果送到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 z ...
- [转载]ASP.NET-----Repeater数据控件的用法总结
一.Repeater控件的用法流程及实例: 1.首先建立一个网站,新建一个网页index.aspx. 2.添加或者建立APP_Data数据文件,然后将用到的数据库文件放到APP_Data文件夹中. 3 ...
- c++第十一天
<c++ primer, 5E> 第68页到第81页,笔记: 1.读取未知量的string对象示例 #include<iostream> using std::cin; usi ...
- ES6学习--函数剩余参数 (rest参数)
ES6 引入 rest 参数(形式为“...变量名”),用于获取函数的多余参数,这样就不需要使用arguments对象了.rest 参数搭配的变量是一个数组,该变量将多余的参数放入数组中.(可以拿到除 ...
- 20145104张家明 《Java程序设计》第四次实验设计
20145104张家明 <Java程序设计>第四次实验设计 这第四次实验报告 我们开始着手安卓了 在电脑上安装了安卓虚拟机