我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用http.cookies.CookieError()。
def format_cookie(cookie): if not cookie: return None try: fmt_headers = {} cookies = Cookie.SimpleCookie(cookie) for key, morsel in six.iteritems(cookies): if "ccsrftoken" in morsel.key: morsel.coded_value = morsel.value fmt_headers["X-CSRFTOKEN"] = morsel.value break fmt_headers["Cookie"] = cookies.output(header="").lstrip() return fmt_headers except (Cookie.CookieError, KeyError): LOG.error(_LE("The cookie ccsrftoken cannot be formatted")) raise Cookie.CookieError
def test_reserved_keys(self): M = cookies.Morsel() # tests valid and invalid reserved keys for Morsels for i in M._reserved: # Test that all valid keys are reported as reserved and set them self.assertTrue(M.isReservedKey(i)) M[i] = '%s_value' % i for i in M._reserved: # Test that valid key values come out fine self.assertEqual(M[i], '%s_value' % i) for i in "the holy hand grenade".split(): # Test that invalid keys raise CookieError self.assertRaises(cookies.CookieError, M.__setitem__, i, '%s_value' % i)
def do_GET(self): # Default message if we don't know a name. message = "I don't know you yet!" # Look for a cookie in the request. if 'cookie' in self.headers: try: # 2. Extract and decode the cookie. # Get the cookie from the headers and extract its value # into a variable called 'name'. # Craft a message, escaping any HTML special chars in name. message = "Hey there, " + html_escape(name) except (KeyError, cookies.CookieError) as e: message = "I'm not sure who you are!" print(e) # First, send a 200 OK response. self.send_response(200) # Then send headers. self.send_header('Content-type', 'text/html; charset=utf-8') self.end_headers() # Send the form with the message in it. mesg = form.format(message) self.wfile.write(mesg.encode())
def do_GET(self): # Default message if we don't know a name. message = "I don't know you yet!" # Look for a cookie in the request. if 'cookie' in self.headers: try: # Extract and decode the cookie. c = cookies.SimpleCookie(self.headers['cookie']) name = c['yourname'].value # Craft a message, escaping any HTML special chars in name. message = "Hey there, " + html_escape(name) except (KeyError, cookies.CookieError) as e: message = "I'm not sure who you are!" print(e) # First, send a 200 OK response. self.send_response(200) # Then send headers. self.send_header('Content-type', 'text/html; charset=utf-8') self.end_headers() # Send the form with the message in it. mesg = form.format(message) self.wfile.write(mesg.encode())