當前位置: 首頁>>代碼示例>>Python>>正文


Python SimpleCookie.load方法代碼示例

本文整理匯總了Python中cherrypy._cpcompat.SimpleCookie.load方法的典型用法代碼示例。如果您正苦於以下問題:Python SimpleCookie.load方法的具體用法?Python SimpleCookie.load怎麽用?Python SimpleCookie.load使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在cherrypy._cpcompat.SimpleCookie的用法示例。


在下文中一共展示了SimpleCookie.load方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Request

# 需要導入模塊: from cherrypy._cpcompat import SimpleCookie [as 別名]
# 或者: from cherrypy._cpcompat.SimpleCookie import load [as 別名]

#.........這裏部分代碼省略.........
            p = httputil.parse_query_string(self.query_string, encoding=self.query_string_encoding)
        except UnicodeDecodeError:
            raise cherrypy.HTTPError(
                404,
                "The given query string could not be processed. Query "
                "strings for this resource must be encoded with %r." % self.query_string_encoding,
            )

        # Python 2 only: keyword arguments must be byte strings (type 'str').
        if not py3k:
            for key, value in p.items():
                if isinstance(key, unicode):
                    del p[key]
                    p[key.encode(self.query_string_encoding)] = value
        self.params.update(p)

    def process_headers(self):
        """Parse HTTP header data into Python structures. (Core)"""
        # Process the headers into self.headers
        headers = self.headers
        for name, value in self.header_list:
            # Call title() now (and use dict.__method__(headers))
            # so title doesn't have to be called twice.
            name = name.title()
            value = value.strip()

            # Warning: if there is more than one header entry for cookies (AFAIK,
            # only Konqueror does that), only the last one will remain in headers
            # (but they will be correctly stored in request.cookie).
            if "=?" in value:
                dict.__setitem__(headers, name, httputil.decode_TEXT(value))
            else:
                dict.__setitem__(headers, name, value)

            # Handle cookies differently because on Konqueror, multiple
            # cookies come on different lines with the same key
            if name == "Cookie":
                try:
                    self.cookie.load(value)
                except CookieError:
                    msg = "Illegal cookie name %s" % value.split("=")[0]
                    raise cherrypy.HTTPError(400, msg)

        if not dict.__contains__(headers, "Host"):
            # All Internet-based HTTP/1.1 servers MUST respond with a 400
            # (Bad Request) status code to any HTTP/1.1 request message
            # which lacks a Host header field.
            if self.protocol >= (1, 1):
                msg = "HTTP/1.1 requires a 'Host' request header."
                raise cherrypy.HTTPError(400, msg)
        host = dict.get(headers, "Host")
        if not host:
            host = self.local.name or self.local.ip
        self.base = "%s://%s" % (self.scheme, host)

    def get_resource(self, path):
        """Call a dispatcher (which sets self.handler and .config). (Core)"""
        # First, see if there is a custom dispatch at this URI. Custom
        # dispatchers can only be specified in app.config, not in _cp_config
        # (since custom dispatchers may not even have an app.root).
        dispatch = self.app.find_config(path, "request.dispatch", self.dispatch)

        # dispatch() should set self.handler and self.config
        dispatch(path)

    def handle_error(self):
        """Handle the last unanticipated exception. (Core)"""
        try:
            self.hooks.run("before_error_response")
            if self.error_response:
                self.error_response()
            self.hooks.run("after_error_response")
            cherrypy.serving.response.finalize()
        except cherrypy.HTTPRedirect:
            inst = sys.exc_info()[1]
            inst.set_response()
            cherrypy.serving.response.finalize()

    # ------------------------- Properties ------------------------- #

    def _get_body_params(self):
        warnings.warn(
            "body_params is deprecated in CherryPy 3.2, will be removed in " "CherryPy 3.3.", DeprecationWarning
        )
        return self.body.params

    body_params = property(
        _get_body_params,
        doc="""
    If the request Content-Type is 'application/x-www-form-urlencoded' or
    multipart, this will be a dict of the params pulled from the entity
    body; that is, it will be the portion of request.params that come
    from the message body (sometimes called "POST params", although they
    can be sent with various HTTP method verbs). This value is set between
    the 'before_request_body' and 'before_handler' hooks (assuming that
    process_request_body is True).

    Deprecated in 3.2, will be removed for 3.3 in favor of
    :attr:`request.body.params<cherrypy._cprequest.RequestBody.params>`.""",
    )
開發者ID:429eagle,項目名稱:BitTorrent.bundle,代碼行數:104,代碼來源:_cprequest.py

示例2: Request

# 需要導入模塊: from cherrypy._cpcompat import SimpleCookie [as 別名]
# 或者: from cherrypy._cpcompat.SimpleCookie import load [as 別名]

#.........這裏部分代碼省略.........
                self.toolmaps = {}
                self.stage = 'get_resource'
                self.get_resource(path_info)
                self.body = _cpreqbody.RequestBody(self.rfile, self.headers, request_params=self.params)
                self.namespaces(self.config)
                self.stage = 'on_start_resource'
                self.hooks.run('on_start_resource')
                self.stage = 'process_query_string'
                self.process_query_string()
                if self.process_request_body:
                    if self.method not in self.methods_with_bodies:
                        self.process_request_body = False
                self.stage = 'before_request_body'
                self.hooks.run('before_request_body')
                if self.process_request_body:
                    self.body.process()
                self.stage = 'before_handler'
                self.hooks.run('before_handler')
                if self.handler:
                    self.stage = 'handler'
                    response.body = self.handler()
                self.stage = 'before_finalize'
                self.hooks.run('before_finalize')
                response.finalize()
            except (cherrypy.HTTPRedirect, cherrypy.HTTPError):
                inst = sys.exc_info()[1]
                inst.set_response()
                self.stage = 'before_finalize (HTTPError)'
                self.hooks.run('before_finalize')
                response.finalize()
            finally:
                self.stage = 'on_end_resource'
                self.hooks.run('on_end_resource')

        except self.throws:
            raise
        except:
            if self.throw_errors:
                raise
            self.handle_error()

    def process_query_string(self):
        try:
            p = httputil.parse_query_string(self.query_string, encoding=self.query_string_encoding)
        except UnicodeDecodeError:
            raise cherrypy.HTTPError(404, 'The given query string could not be processed. Query strings for this resource must be encoded with %r.' % self.query_string_encoding)

        for key, value in p.items():
            if isinstance(key, unicode):
                del p[key]
                p[key.encode(self.query_string_encoding)] = value

        self.params.update(p)

    def process_headers(self):
        headers = self.headers
        for name, value in self.header_list:
            name = name.title()
            value = value.strip()
            if '=?' in value:
                dict.__setitem__(headers, name, httputil.decode_TEXT(value))
            else:
                dict.__setitem__(headers, name, value)
            if name == 'Cookie':
                try:
                    self.cookie.load(value)
                except CookieError:
                    msg = 'Illegal cookie name %s' % value.split('=')[0]
                    raise cherrypy.HTTPError(400, msg)

        if not dict.__contains__(headers, 'Host'):
            if self.protocol >= (1, 1):
                msg = "HTTP/1.1 requires a 'Host' request header."
                raise cherrypy.HTTPError(400, msg)
        host = dict.get(headers, 'Host')
        if not host:
            host = self.local.name or self.local.ip
        self.base = '%s://%s' % (self.scheme, host)

    def get_resource(self, path):
        dispatch = self.app.find_config(path, 'request.dispatch', self.dispatch)
        dispatch(path)

    def handle_error(self):
        try:
            self.hooks.run('before_error_response')
            if self.error_response:
                self.error_response()
            self.hooks.run('after_error_response')
            cherrypy.serving.response.finalize()
        except cherrypy.HTTPRedirect:
            inst = sys.exc_info()[1]
            inst.set_response()
            cherrypy.serving.response.finalize()

    def _get_body_params(self):
        warnings.warn('body_params is deprecated in CherryPy 3.2, will be removed in CherryPy 3.3.', DeprecationWarning)
        return self.body.params

    body_params = property(_get_body_params, doc='\n    If the request Content-Type is \'application/x-www-form-urlencoded\' or\n    multipart, this will be a dict of the params pulled from the entity\n    body; that is, it will be the portion of request.params that come\n    from the message body (sometimes called "POST params", although they\n    can be sent with various HTTP method verbs). This value is set between\n    the \'before_request_body\' and \'before_handler\' hooks (assuming that\n    process_request_body is True).\n    \n    Deprecated in 3.2, will be removed for 3.3 in favor of\n    :attr:`request.body.params<cherrypy._cprequest.RequestBody.params>`.')
開發者ID:connoryang,項目名稱:dec-eve-serenity,代碼行數:104,代碼來源:_cprequest.py


注:本文中的cherrypy._cpcompat.SimpleCookie.load方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。