Requests教程-2-源码解析

上一小节中,我们完成了requests库的环境搭建,并且了解了如何使用requests发送接口请求,本节中我们对requests发送请求的源码进行分析。

首先明确一点,requests发送请求,其实是内部调用了requests.request方法,如下图,按住ctrl键盘点击request方法即可进入该方法的源码。

源码如下:

def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.

    :param method: method for the new :class:`Request` object: ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the query string for the :class:`Request`.
    :param data: (optional) Dictionary, list of tuples, 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.
    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
    :param timeout: (optional) How many seconds to wait for the server to send data
        before giving up, as a float, or a :ref:`(connect timeout, read
        timeout) <timeouts>` tuple.
    :type timeout: float or tuple
    :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
    :type allow_redirects: bool
    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
    :param 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``.
    :param stream: (optional) if ``False``, the response content will be immediately downloaded.
    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response

    Usage::

      >>> import requests
      >>> req = requests.request('GET', 'https://httpbin.org/get')
      >>> req
      <Response [200]>
    """

    # By using the 'with' statement we are sure the session is closed, thus we
    # avoid leaving sockets open which can trigger a ResourceWarning in some
    # cases, and look like a memory leak in others.
    with sessions.Session() as session:
        return session.request(method=method, url=url, **kwargs)

上面的源码注释部分,param表示该方法的入参种类,接下来我们具体分析一下, request方法这15个入参分别代表什么含义,后续编写接口请求时,做到心中有数。

  • method: 表示接口请求方式 ,常见的有get/post/put/delete 等
  • url :表示接口请求的 url 地址
  • params:表示接口请求数据,常用于get请求中,数据放在url中
  • data :表示接口请求数据,数据格式为表单,常用于post请求
  • json:表示接口请求数据,数据格式为json,常用于post请求
  • headers: 表示接口请求头信息,http请求中,比如说编码方式等内容添加
  • cookie :表示保存的用户个人信息,如登录信息
  • file: 表示上传文件接口中需要传该参数信息
  • auth :表示鉴权的意思,接口设置操作权限
  • timeout :表示超时处理 ,可设置超时时间
  • proxys:表示设置代理
  • allow_redirects :表示重定向,请求不成功,再次请求
  • verify :证书验证 1、要么请求忽略证书,2、要么加载证书地址
  • stream :表示下载文件接口中需要传该参数信息
  • cert :表示CA证书参数

相关推荐

  1. spring oauth2 authorization server 配置

    2024-01-08 22:46:01       55 阅读
  2. vue3、vue2中nextTick

    2024-01-08 22:46:01       50 阅读
  3. SpringBoot

    2024-01-08 22:46:01       60 阅读
  4. ConcurrentHashMap

    2024-01-08 22:46:01       66 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-08 22:46:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-08 22:46:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-08 22:46:01       82 阅读
  4. Python语言-面向对象

    2024-01-08 22:46:01       91 阅读

热门阅读

  1. k8s-master增加和删除污点

    2024-01-08 22:46:01       56 阅读
  2. 开启鸿蒙开发探索之旅

    2024-01-08 22:46:01       58 阅读
  3. python爬虫

    2024-01-08 22:46:01       66 阅读
  4. Linux常用命令大全<二>

    2024-01-08 22:46:01       60 阅读
  5. 阿里云服务器配置选择方案

    2024-01-08 22:46:01       67 阅读
  6. Rust组织下的其他项目介绍

    2024-01-08 22:46:01       47 阅读
  7. 零信任 aTrust 系统升级

    2024-01-08 22:46:01       82 阅读
  8. 网站遇到DDOS攻击怎么办

    2024-01-08 22:46:01       56 阅读
  9. 闲聊篇-求职的点点滴滴~~

    2024-01-08 22:46:01       57 阅读