Python 理解烧瓶请求对象

Python 理解烧瓶请求对象,python,flask,Python,Flask,我想了解Flask中的请求对象是如何工作的。具体来说,通过查看下面的代码,摘自。 我的问题是:请求对象与实际请求之间的链接在哪里 换句话说,request.is_json如何知道它应该指向哪些数据(通过请求发送的数据) 谢谢你的帮助 如果我理解正确,用评论中给出的细节回答你的问题 request对象是在您第一次启动Flask服务器时创建的,但是,Flask会跟踪请求上下文堆栈,所有请求都会在那里结束 然后flask调用url的特定端点,您可以从该端点访问请求对象。由于flask实际上使用了来

我想了解
Flask
中的
请求
对象是如何工作的。具体来说,通过查看下面的代码,摘自。

我的问题是:
请求
对象与实际请求之间的链接在哪里

换句话说,
request.is_json
如何知道它应该指向哪些数据(通过请求发送的数据)


谢谢你的帮助

如果我理解正确,用评论中给出的细节回答你的问题

request
对象是在您第一次启动
Flask
服务器时创建的,但是,Flask会跟踪请求上下文堆栈,所有请求都会在那里结束

然后flask调用url的特定端点,您可以从该端点访问请求对象。由于flask实际上使用了来自werkzeug的
BaseRequest
对象,因此它继承了
get\u data
方法,该方法将请求数据反序列化以供以后解析

特定的请求对象再次使用继承的mixin来区分json和其他内容

class Request(RequestBase, JSONMixin):
    """The request object used by default in Flask.  Remembers the
    matched endpoint and view arguments.
    It is what ends up as :class:`~flask.request`.  If you want to replace
    the request object used you can subclass this and set
    :attr:`~flask.Flask.request_class` to your subclass.
    The request object is a :class:`~werkzeug.wrappers.Request` subclass and
    provides all of the attributes Werkzeug defines plus a few Flask
    specific ones.

如果您想了解更多信息,请随时继续阅读源代码。如果您有任何问题,请留下评论。

如果您对
请求
对象的创建方式感兴趣,请随时阅读源代码。是否有任何与您的问题相关的具体问题,我们可以帮助您解决?感谢@PaxVobiscum的评论。我是Flask的新手,我只是想了解请求对象如何知道在哪里查找数据。或者,也许这种方式更清楚,在代码的哪一行,包含来自客户机请求的信息的请求对象出现/变为可用?也许唯一的解释是源代码,我不知道。我不确定我是否理解你的问题,或者你为什么想知道这个。您想要行号吗?在第8行中有一个请求对象,该对象指向已请求的实际数据,对吗?这是我的问题,在哪里/何时进行连接。它是请求对象创建时固有的东西吗?所以当这个对象从Flask导入时,它已经知道如果有请求,这个对象应该指向请求中提供的数据(POST请求)?我希望你现在能更好地理解我。这可能有点不必要的担心,我同意这一点。
def get_data(self, as_text=False):
    """The string representation of the request body.  Whenever you call
    this property the request iterable is encoded and flattened.  This
    can lead to unwanted behavior if you stream big data.
    This behavior can be disabled by setting
    :attr:`implicit_sequence_conversion` to `False`.
    If `as_text` is set to `True` the return value will be a decoded
    unicode string.
    .. versionadded:: 0.9
    """
    self._ensure_sequence()
    rv = b''.join(self.iter_encoded())
    if as_text:
        rv = rv.decode(self.charset)
    return rv
class Request(RequestBase, JSONMixin):
    """The request object used by default in Flask.  Remembers the
    matched endpoint and view arguments.
    It is what ends up as :class:`~flask.request`.  If you want to replace
    the request object used you can subclass this and set
    :attr:`~flask.Flask.request_class` to your subclass.
    The request object is a :class:`~werkzeug.wrappers.Request` subclass and
    provides all of the attributes Werkzeug defines plus a few Flask
    specific ones.