第三方库requests理解及post请求实现

接口自动化使用requests包,本片文章通过底层代码理解requests如何使用文章目录一、requests是什么?二、第三方库requests解析1.requests调用api.py中的request, get, head, post, patch, put, delete, options等方法2.进一步查看api.py文件3.实践:通过一个POST请求接口,使用第三方库requests实现总结一、requests是什么?Requests 是用Python语言编写,基于 urllib.
摘要由CSDN通过智能技术生成

接口自动化使用requests包,本片文章通过底层代码理解requests如何使用



一、requests是什么?

Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库。它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求。Requests 的哲学是以 PEP 20 的习语为中心开发的,所以它比 urllib 更加 Pythoner。更重要的一点是它支持 Python3 哦!

二、第三方库requests解析

1.requests调用api.py中的request, get, head, post, patch, put, delete, options等方法

代码如下(示例):

    def test_get(self):
        r = requests.get("http://www.baidu.com")
 点击requests跳转至init.py中:
    from .api import request, get, head, post, patch, put, delete, options
 显示第三方库requests 是通过调用api.py文件中的一系列方法构成,可以直接调用这些方法

2.进一步查看api.py文件

代码如下(示例):

This module implements the Requests API.
包含以下函数:
def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.

    :param method: method for the new :class:`Request` object.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
    :param 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 :class:`Request`.
    :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
    :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
    :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
    :param 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.
    :
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值