Django_WSGIRequest對象

WSGIRequest對象

Django在接收到http請求以後,會根據http請求攜帶的參數以及報文信息建立一個WSGIRequest對象,而且做爲視圖函數第一個參數傳給視圖函數。這個參數就是django視圖函數的第一個參數,一般寫成request。在這個對象上咱們能夠找到客戶端上傳上來的全部信息。這個對象的完整路徑是django.core.handlers.wsgi.WSGIRequesthtml

class WSGIRequest(HttpRequest):
    def __init__(self, environ):
        script_name = get_script_name(environ)
        path_info = get_path_info(environ)
        if not path_info:
            # Sometimes PATH_INFO exists, but is empty (e.g. accessing
            # the SCRIPT_NAME URL without a trailing slash). We really need to
            # operate as if they'd requested '/'. Not amazingly nice to force
            # the path like this, but should be harmless.
            path_info = '/'
        self.environ = environ
        self.path_info = path_info
        # be careful to only replace the first slash in the path because of
        # http://test/something and http://test//something being different as
        # stated in http://www.ietf.org/rfc/rfc2396.txt
        self.path = '%s/%s' % (script_name.rstrip('/'),
                               path_info.replace('/', '', 1))
        self.META = environ
        self.META['PATH_INFO'] = path_info
        self.META['SCRIPT_NAME'] = script_name
        self.method = environ['REQUEST_METHOD'].upper()
        self.content_type, self.content_params = cgi.parse_header(environ.get('CONTENT_TYPE', ''))
        if 'charset' in self.content_params:
            try:
                codecs.lookup(self.content_params['charset'])
            except LookupError:
                pass
            else:
                self.encoding = self.content_params['charset']
        self._post_parse_error = False
        try:
            content_length = int(environ.get('CONTENT_LENGTH'))
        except (ValueError, TypeError):
            content_length = 0
        self._stream = LimitedStream(self.environ['wsgi.input'], content_length)
        self._read_started = False
        self.resolver_match = None

    def _get_scheme(self):
        return self.environ.get('wsgi.url_scheme')

    @cached_property
    def GET(self):
        # The WSGI spec says 'QUERY_STRING' may be absent.
        raw_query_string = get_bytes_from_wsgi(self.environ, 'QUERY_STRING', '')
        return QueryDict(raw_query_string, encoding=self._encoding)

    def _get_post(self):
        if not hasattr(self, '_post'):
            self._load_post_and_files()
        return self._post

    def _set_post(self, post):
        self._post = post

    @cached_property
    def COOKIES(self):
        raw_cookie = get_str_from_wsgi(self.environ, 'HTTP_COOKIE', '')
        return parse_cookie(raw_cookie)

    @property
    def FILES(self):
        if not hasattr(self, '_files'):
            self._load_post_and_files()
        return self._files

    POST = property(_get_post, _set_post)






class HttpRequest:
    """A basic HTTP request."""

    # The encoding used in GET/POST dicts. None means use default setting.
    _encoding = None
    _upload_handlers = []

    def __init__(self):
        # WARNING: The `WSGIRequest` subclass doesn't call `super`.
        # Any variable assignment made here should also happen in
        # `WSGIRequest.__init__()`.

        self.GET = QueryDict(mutable=True)
        self.POST = QueryDict(mutable=True)
        self.COOKIES = {}
        self.META = {}
        self.FILES = MultiValueDict()

        self.path = ''
        self.path_info = ''
        self.method = None
        self.resolver_match = None
        self._post_parse_error = False
        self.content_type = None
        self.content_params = None

    def __repr__(self):
        if self.method is None or not self.get_full_path():
            return '<%s>' % self.__class__.__name__
        return '<%s: %s %r>' % (self.__class__.__name__, self.method, self.get_full_path())

    def _get_raw_host(self):
        """
        Return the HTTP host using the environment or request headers. Skip
        allowed hosts protection, so may return an insecure host.
        """
        # We try three options, in order of decreasing preference.
        if settings.USE_X_FORWARDED_HOST and (
                'HTTP_X_FORWARDED_HOST' in self.META):
            host = self.META['HTTP_X_FORWARDED_HOST']
        elif 'HTTP_HOST' in self.META:
            host = self.META['HTTP_HOST']
        else:
            # Reconstruct the host using the algorithm from PEP 333.
            host = self.META['SERVER_NAME']
            server_port = self.get_port()
            if server_port != ('443' if self.is_secure() else '80'):
                host = '%s:%s' % (host, server_port)
        return host

    def get_host(self):
        """Return the HTTP host using the environment or request headers."""
        host = self._get_raw_host()

        # Allow variants of localhost if ALLOWED_HOSTS is empty and DEBUG=True.
        allowed_hosts = settings.ALLOWED_HOSTS
        if settings.DEBUG and not allowed_hosts:
            allowed_hosts = ['localhost', '127.0.0.1', '[::1]']

        domain, port = split_domain_port(host)
        if domain and validate_host(domain, allowed_hosts):
            return host
        else:
            msg = "Invalid HTTP_HOST header: %r." % host
            if domain:
                msg += " You may need to add %r to ALLOWED_HOSTS." % domain
            else:
                msg += " The domain name provided is not valid according to RFC 1034/1035."
            raise DisallowedHost(msg)

    def get_port(self):
        """Return the port number for the request as a string."""
        if settings.USE_X_FORWARDED_PORT and 'HTTP_X_FORWARDED_PORT' in self.META:
            port = self.META['HTTP_X_FORWARDED_PORT']
        else:
            port = self.META['SERVER_PORT']
        return str(port)

    def get_full_path(self, force_append_slash=False):
        # RFC 3986 requires query string arguments to be in the ASCII range.
        # Rather than crash if this doesn't happen, we encode defensively.
        return '%s%s%s' % (
            escape_uri_path(self.path),
            '/' if force_append_slash and not self.path.endswith('/') else '',
            ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) if self.META.get('QUERY_STRING', '') else ''
        )

    def get_signed_cookie(self, key, default=RAISE_ERROR, salt='', max_age=None):
        """
        Attempt to return a signed cookie. If the signature fails or the
        cookie has expired, raise an exception, unless the `default` argument
        is provided,  in which case return that value.
        """
        try:
            cookie_value = self.COOKIES[key]
        except KeyError:
            if default is not RAISE_ERROR:
                return default
            else:
                raise
        try:
            value = signing.get_cookie_signer(salt=key + salt).unsign(
                cookie_value, max_age=max_age)
        except signing.BadSignature:
            if default is not RAISE_ERROR:
                return default
            else:
                raise
        return value

    def get_raw_uri(self):
        """
        Return an absolute URI from variables available in this request. Skip
        allowed hosts protection, so may return insecure URI.
        """
        return '{scheme}://{host}{path}'.format(
            scheme=self.scheme,
            host=self._get_raw_host(),
            path=self.get_full_path(),
        )

    def build_absolute_uri(self, location=None):
        """
        Build an absolute URI from the location and the variables available in
        this request. If no ``location`` is specified, bulid the absolute URI
        using request.get_full_path(). If the location is absolute, convert it
        to an RFC 3987 compliant URI and return it. If location is relative or
        is scheme-relative (i.e., ``//example.com/``), urljoin() it to a base
        URL constructed from the request variables.
        """
        if location is None:
            # Make it an absolute url (but schemeless and domainless) for the
            # edge case that the path starts with '//'.
            location = '//%s' % self.get_full_path()
        bits = urlsplit(location)
        if not (bits.scheme and bits.netloc):
            current_uri = '{scheme}://{host}{path}'.format(scheme=self.scheme,
                                                           host=self.get_host(),
                                                           path=self.path)
            # Join the constructed URL with the provided location, which will
            # allow the provided ``location`` to apply query strings to the
            # base path as well as override the host, if it begins with //
            location = urljoin(current_uri, location)
        return iri_to_uri(location)

    def _get_scheme(self):
        """
        Hook for subclasses like WSGIRequest to implement. Return 'http' by
        default.
        """
        return 'http'

    @property
    def scheme(self):
        if settings.SECURE_PROXY_SSL_HEADER:
            try:
                header, value = settings.SECURE_PROXY_SSL_HEADER
            except ValueError:
                raise ImproperlyConfigured(
                    'The SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.'
                )
            if self.META.get(header) == value:
                return 'https'
        return self._get_scheme()

    def is_secure(self):
        return self.scheme == 'https'

    def is_ajax(self):
        return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

    @property
    def encoding(self):
        return self._encoding

    @encoding.setter
    def encoding(self, val):
        """
        Set the encoding used for GET/POST accesses. If the GET or POST
        dictionary has already been created, remove and recreate it on the
        next access (so that it is decoded correctly).
        """
        self._encoding = val
        if hasattr(self, 'GET'):
            del self.GET
        if hasattr(self, '_post'):
            del self._post

    def _initialize_handlers(self):
        self._upload_handlers = [uploadhandler.load_handler(handler, self)
                                 for handler in settings.FILE_UPLOAD_HANDLERS]

    @property
    def upload_handlers(self):
        if not self._upload_handlers:
            # If there are no upload handlers defined, initialize them from settings.
            self._initialize_handlers()
        return self._upload_handlers

    @upload_handlers.setter
    def upload_handlers(self, upload_handlers):
        if hasattr(self, '_files'):
            raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
        self._upload_handlers = upload_handlers

    def parse_file_upload(self, META, post_data):
        """Return a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse()

    @property
    def body(self):
        if not hasattr(self, '_body'):
            if self._read_started:
                raise RawPostDataException("You cannot access body after reading from request's data stream")

            # Limit the maximum request data size that will be handled in-memory.
            if (settings.DATA_UPLOAD_MAX_MEMORY_SIZE is not None and
                    int(self.META.get('CONTENT_LENGTH') or 0) > settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
                raise RequestDataTooBig('Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.')

            try:
                self._body = self.read()
            except IOError as e:
                raise UnreadablePostError(*e.args) from e
            self._stream = BytesIO(self._body)
        return self._body

    def _mark_post_parse_error(self):
        self._post = QueryDict()
        self._files = MultiValueDict()
        self._post_parse_error = True

    def _load_post_and_files(self):
        """Populate self._post and self._files if the content-type is a form type"""
        if self.method != 'POST':
            self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict()
            return
        if self._read_started and not hasattr(self, '_body'):
            self._mark_post_parse_error()
            return

        if self.content_type == 'multipart/form-data':
            if hasattr(self, '_body'):
                # Use already read data
                data = BytesIO(self._body)
            else:
                data = self
            try:
                self._post, self._files = self.parse_file_upload(self.META, data)
            except MultiPartParserError:
                # An error occurred while parsing POST data. Since when
                # formatting the error the request handler might access
                # self.POST, set self._post and self._file to prevent
                # attempts to parse POST data again.
                # Mark that an error occurred. This allows self.__repr__ to
                # be explicit about it instead of simply representing an
                # empty POST
                self._mark_post_parse_error()
                raise
        elif self.content_type == 'application/x-www-form-urlencoded':
            self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()
        else:
            self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict()

    def close(self):
        if hasattr(self, '_files'):
            for f in chain.from_iterable(l[1] for l in self._files.lists()):
                f.close()

    # File-like and iterator interface.
    #
    # Expects self._stream to be set to an appropriate source of bytes by
    # a corresponding request subclass (e.g. WSGIRequest).
    # Also when request data has already been read by request.POST or
    # request.body, self._stream points to a BytesIO instance
    # containing that data.

    def read(self, *args, **kwargs):
        self._read_started = True
        try:
            return self._stream.read(*args, **kwargs)
        except IOError as e:
            raise UnreadablePostError(*e.args) from e

    def readline(self, *args, **kwargs):
        self._read_started = True
        try:
            return self._stream.readline(*args, **kwargs)
        except IOError as e:
            raise UnreadablePostError(*e.args) from e

    def __iter__(self):
        while True:
            buf = self.readline()
            if not buf:
                break
            yield buf

    def xreadlines(self):
        warnings.warn(
            'HttpRequest.xreadlines() is deprecated in favor of iterating the '
            'request.', RemovedInDjango30Warning, stacklevel=2,
        )
        yield from self

    def readlines(self):
        return list(self)
WSGIRequest源碼 + HttpRequest源碼

 

http請求的url詳解:

在瞭解WSGIRequest對象的屬性和方法以前,咱們先了解一下url的組成,一般來講url的完整組成以下,[]爲可選:python

protocol ://hostname[:port]/path/[;parameters][?query]#fragmentnginx

  • protocol: 網絡協議,經常使用的協議有http/https/ftp等
  • hostname: 主機地址,能夠是域名,也能夠是IP地址
  • port: 端口 http協議默認端口是:80端口,在瀏覽器中默認會隱藏不顯示
  • path:路徑 網絡資源在服務器中的指定路徑
  • parameter: 參數 若是要向服務器傳入參數,在這部分輸入
  • query: 查詢字符串 若是須要從服務器那裏查詢內容,在這裏編輯
  • fragment:片斷 網頁中可能會分爲不一樣的片斷,若是想訪問網頁後直接到達指定位置,能夠在這部分設置

 

WSGIRequest對象經常使用屬性:

WSGIRequest對象上大部分的屬性都是隻讀的。由於這些屬性是從客戶端上傳上來的,不必作任何的修改,在django視圖中使用時,視圖函數的第一個參數參數request就是WSGIRequest對象。如下將對一些經常使用的屬性進行講解:ajax

  1. path:資源在服務器的完整「路徑」,但不包含域名和參數,在url中也是path的內容。好比http://www.baidu.com/xxx/yyy/,那麼path就是/xxx/yyy/
  2. method:表明當前請求的http方法。好比是GET、POST、delete或者是put等方法
  3. GET:一個django.http.request.QueryDict對象。操做起來相似於字典。這個屬性中包含了全部以?xxx=xxx的方式上傳上來的參數。
  4. POST:也是一個django.http.request.QueryDict對象。這個屬性中包含了全部以POST方式上傳上來的參數。
  5. FILES:也是一個django.http.request.QueryDict對象。這個屬性中包含了全部上傳的文件。
  6. COOKIES:一個標準的Python字典,包含全部的cookie,鍵值對都是字符串類型。
  7. session:一個相似於字典的對象。用來操做服務器的session
  8. user:user 只有當Django 啓用 AuthenticationMiddleware 中間件時纔可用。它的值是一個 setting.py 裏面AUTH_USER_MODEL 字段所定義的類的對象,表示當前登陸的用戶。若是用戶當前沒有登陸,user 將設爲 django.contrib.auth.models.AnonymousUser 的一個實例。你能夠經過 is_authenticated() 區分它們。django

  9. META:存儲的客戶端發送上來的全部header信息,下面是這些經常使用的header信息:瀏覽器

    1.   CONTENT_LENGTH:請求的正文的長度(是一個字符串)。服務器

    2.   CONTENT_TYPE:請求的正文的MIME類型。
    3.   HTTP_ACCEPT:響應可接收的Content-Type。
    4.   HTTP_ACCEPT_ENCODING:響應可接收的編碼,用於告知服務器客戶端所可以處理的編碼方式和相對優先級。
    5.   HTTP_ACCEPT_LANGUAGE: 響應可接收的語言。
    6.   HTTP_HOST:客戶端發送的HOST值。
    7.   HTTP_REFERER:在訪問這個頁面上一個頁面的url。
    8.   QUERY_STRING:單個字符串形式的查詢字符串(未解析過的形式)。
    9.   TE:設置傳輸實體的編碼格式,表示請求發起者願意接收的Transfer-Encoding類型(傳輸過程當中的編碼格式,代理服務器之間)
    10.   REMOTE_ADDR:客戶端的IP地址。若是服務器使用了nginx作反向代理或者負載均衡,那麼這個值返回的是127.0.0.1,這時候可使用HTTP_X_FORWARDED_FOR來獲取,因此獲取ip地址的代碼片斷以下:
      if request.META.has_key('HTTP_X_FORWARDED_FOR'): ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] 
    11.   REMOTE_HOST:客戶端的主機名。
    12.   REQUEST_METHOD:請求方法。一個字符串相似於GET或者POST
    13.   SERVER_NAME:服務器域名。
    14.   SERVER_PORT:服務器端口號,是一個字符串類型。

WSGIRequest對象經常使用方法:

  1. is_secure():是不是採用https協議。
  2. is_ajax():是否採用ajax發送的請求。原理就是判斷請求頭中是否存在X-Requested-With:XMLHttpRequest
  3. get_host():服務器的域名。若是在訪問的時候還有端口號,那麼會加上端口號,在url中就是hostname+port。好比www.baidu.com:9000
  4. get_full_path():返回完整的path。若是有查詢字符串,還會加上查詢字符串,在url中就是path以及其後面的全部。好比/music/bands/?print=True
  5. get_raw_uri():獲取請求的完整url

QueryDict對象:

用法:http://www.javashuo.com/article/p-kpqhxshw-da.htmlcookie

咱們平時用的request.GET、request.POST和request.FILES都是QueryDict對象,這個對象繼承自dict,所以用法跟dict相差無幾。其中用得比較多的是get方法和getlist方法。網絡

  1. get方法:用來獲取指定key的值,若是沒有這個key,那麼會返回None
  2. getlist方法:若是瀏覽器上傳上來的key對應的值有多個,若是使用get取值,那麼你只能取出最後面一個值,若是你想取到全部的值,那麼就須要經過getlist這個方法獲取。

 

 

轉發or參考:

http://www.javashuo.com/article/p-bytvmqoz-dt.htmlsession

相關文章
相關標籤/搜索