本文为博主原创,未经许可严禁转载。
本文链接:https://blog.csdn.net/zyooooxie/article/details/109101720
前些天 看requests官方文档 , 有些想法,做个分享
这是之前已写 关于接口测试的category ,有兴趣可以看看。
个人博客:https://blog.csdn.net/zyooooxie
Response对象、Request对象
看到这2个地方:
- 使用 requests 发请求时,实际构建了一个 Request 对象;
- 一旦 requests 得到一个从服务器返回的响应,就会产生一个 Response 对象。该响应对象包含服务器返回的所有信息,也包含你原来创建的 Request 对象
- 收到一个 Response 对象时,request 属性 其实是使用了 PreparedRequest
实际请求,看看:
确实如此。
就是说 上图的res 是一个Response对象 ; 发送请求的Request 对象是res.request;
回归源码去看看:
源码- Response类的文档注释、_init_方法
class Response(object):
"""The :class:`Response <Response>` object, which contains a
server's response to an HTTP request.
"""
__attrs__ = [
'_content', 'status_code', 'headers', 'url', 'history',
'encoding', 'reason', 'cookies', 'elapsed', 'request'
]
def __init__(self):
self._content = False
self._content_consumed = False
self._next = None
#: Integer Code of responded HTTP Status, e.g. 404 or 200.
self.status_code = None
#: Case-insensitive Dictionary of Response Headers.
#: For example, ``headers['content-encoding']`` will return the
#: value of a ``'Content-Encoding'`` response header.
self.headers = CaseInsensitiveDict()
#: File-like object representation of response (for advanced usage).
#: Use of ``raw`` requires that ``stream=True`` be set on the request.
# This requirement does not apply for use internally to Requests.
self.raw = None
#: Final URL location of Response.
self.url = None
#: Encoding to decode with when accessing r.text.
self.encoding = None
#: A list of :class:`Response <Response>` objects from
#: the history of the Request. Any redirect responses will end
#: up here. The list is sorted from the oldest to the most recent request.
self.history = []
#: Textual reason of responded HTTP Status, e.g. "Not Found" or "OK".
self.reason = None
#: A CookieJar of Cookies the server sent back.
self.cookies = cookiejar_from_dict({})
#: The amount of time elapsed between sending the request
#: and the arrival of the response (as a timedelta).
#: This property specifically measures the time taken between sending
#: the first byte of the request and finishing parsing the headers. It
#: is therefore unaffected by consuming the response content or the
#: value of the ``stream`` keyword argument.
self.elapsed = datetime.timedelta(0)
#: The :class:`PreparedRequest <PreparedRequest>` object to which this
#: is a response.
self.request = None
看到 定义的一些属性、方法,就真的有些豁然开朗的感觉 【应该是 不明觉厉】
然后 我自己整理了些:
源码 PreparedRequest类 的文档注释、_init_方法
class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
"""The fully mutable :class:`PreparedRequest <PreparedRequest>` object,
containing the exact bytes that will be sent to the server.
Generated from either a :class:`Request <Request>` object or manually.
Usage::
>>> import requests
>>> req = requests.Request('GET', 'https://httpbin.org/get')
>>> r = req.prepare()
<PreparedRequest [GET]>
>>> s = requests.Session()
>>> s.send(r)
<Response [200]>
"""
def __init__(self):
#: HTTP verb to send to the server.
self.method = None
#: HTTP URL to send the request to.
self.url = None
#: dictionary of HTTP headers.
self.headers = None
# The `CookieJar` used to create the Cookie header will be stored here
# after prepare_cookies is called
self._cookies = None
#: request body to send to the server.
self.body = None
#: dictionary of callback hooks, for internal usage.
self.hooks = default_hooks()
#: integer denoting starting position of a readable file-like body.
self._body_position = None
就涵盖了 请求报文
输出 整个HTTP请求信息
前面说到了Request and Response Objects,但我实际使用requests发请求,想要看这请求的详情,一般使用Fiddler抓包;
后面了解到 requests_toolbelt库,有些方法 很方便;
from requests_toolbelt.utils import dump
Log.info(dump.dump_all(res).decode('utf-8'))
稍微扩展下:
交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie