本文整理汇总了Python中django.http.cookie.SimpleCookie方法的典型用法代码示例。如果您正苦于以下问题:Python cookie.SimpleCookie方法的具体用法?Python cookie.SimpleCookie怎么用?Python cookie.SimpleCookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.cookie
的用法示例。
在下文中一共展示了cookie.SimpleCookie方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def __init__(self, content_type=None, status=None, reason=None, charset=None):
# _headers is a mapping of the lower-case name to the original case of
# the header (required for working with legacy systems) and the header
# value. Both the name of the header and its value are ASCII strings.
self._headers = {}
self._closable_objects = []
# This parameter is set by the handler. It's necessary to preserve the
# historical behavior of request_finished.
self._handler_class = None
self.cookies = SimpleCookie()
self.closed = False
if status is not None:
self.status_code = status
if reason is not None:
self.reason_phrase = reason
elif self.reason_phrase is None:
self.reason_phrase = REASON_PHRASES.get(self.status_code,
'UNKNOWN STATUS CODE')
self._charset = charset
if content_type is None:
content_type = '%s; charset=%s' % (settings.DEFAULT_CONTENT_TYPE,
self.charset)
self['Content-Type'] = content_type
示例2: __init__
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def __init__(self, content_type=None, status=None, reason=None, charset=None):
# _headers is a mapping of the lower-case name to the original case of
# the header (required for working with legacy systems) and the header
# value. Both the name of the header and its value are ASCII strings.
self._headers = {}
self._closable_objects = []
# This parameter is set by the handler. It's necessary to preserve the
# historical behavior of request_finished.
self._handler_class = None
self.cookies = SimpleCookie()
self.closed = False
if status is not None:
try:
self.status_code = int(status)
except (ValueError, TypeError):
raise TypeError('HTTP status code must be an integer.')
if not 100 <= self.status_code <= 599:
raise ValueError('HTTP status code must be an integer from 100 to 599.')
self._reason_phrase = reason
self._charset = charset
if content_type is None:
content_type = '%s; charset=%s' % (settings.DEFAULT_CONTENT_TYPE,
self.charset)
self['Content-Type'] = content_type
示例3: __init__
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def __init__(self, content_type=None, status=None, mimetype=None):
# _headers is a mapping of the lower-case name to the original case of
# the header (required for working with legacy systems) and the header
# value. Both the name of the header and its value are ASCII strings.
self._headers = {}
self._charset = settings.DEFAULT_CHARSET
self._closable_objects = []
# This parameter is set by the handler. It's necessary to preserve the
# historical behavior of request_finished.
self._handler_class = None
if mimetype:
warnings.warn("Using mimetype keyword argument is deprecated, use"
" content_type instead", PendingDeprecationWarning)
content_type = mimetype
if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
self._charset)
self.cookies = SimpleCookie()
if status:
self.status_code = status
self['Content-Type'] = content_type
示例4: __init__
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def __init__(self, content_type=None, status=None, reason=None, charset=None):
# _headers is a mapping of the lower-case name to the original case of
# the header (required for working with legacy systems) and the header
# value. Both the name of the header and its value are ASCII strings.
self._headers = {}
self._closable_objects = []
# This parameter is set by the handler. It's necessary to preserve the
# historical behavior of request_finished.
self._handler_class = None
self.cookies = SimpleCookie()
self.closed = False
if status is not None:
self.status_code = status
self._reason_phrase = reason
self._charset = charset
if content_type is None:
content_type = '%s; charset=%s' % (settings.DEFAULT_CONTENT_TYPE,
self.charset)
self['Content-Type'] = content_type
示例5: request_started_handler
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def request_started_handler(sender, environ, **kwargs):
if not should_log_url(environ['PATH_INFO']):
return
# try and get the user from the request; commented for now, may have a bug in this flow.
# user = get_current_user()
user = None
# get the user from cookies
if not user and environ.get('HTTP_COOKIE'):
cookie = SimpleCookie() # python3 compatibility
cookie.load(environ['HTTP_COOKIE'])
session_cookie_name = settings.SESSION_COOKIE_NAME
if session_cookie_name in cookie:
session_id = cookie[session_cookie_name].value
try:
session = Session.objects.get(session_key=session_id)
except Session.DoesNotExist:
session = None
if session:
user_id = session.get_decoded().get('_auth_user_id')
try:
user = get_user_model().objects.get(id=user_id)
except:
user = None
request_event = audit_logger.request({
'url': environ['PATH_INFO'],
'method': environ['REQUEST_METHOD'],
'query_string': environ['QUERY_STRING'],
'user_id': getattr(user, 'id', None),
'remote_ip': environ[REMOTE_ADDR_HEADER],
'datetime': timezone.now()
})
示例6: __getstate__
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def __getstate__(self):
# SimpleCookie is not pickeable with pickle.HIGHEST_PROTOCOL, so we
# serialise to a string instead
state = self.__dict__.copy()
state['cookies'] = str(state['cookies'])
return state
示例7: __setstate__
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def __setstate__(self, state):
self.__dict__.update(state)
self.cookies = SimpleCookie(self.cookies)
示例8: test_transaction_request_response_data
# 需要导入模块: from django.http import cookie [as 别名]
# 或者: from django.http.cookie import SimpleCookie [as 别名]
def test_transaction_request_response_data(django_elasticapm_client, client):
client.cookies = SimpleCookie({"foo": "bar"})
with override_settings(
**middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.TracingMiddleware"])
):
client.get(reverse("elasticapm-no-error"))
transactions = django_elasticapm_client.events[TRANSACTION]
assert len(transactions) == 1
transaction = transactions[0]
assert transaction["result"] == "HTTP 2xx"
assert "request" in transaction["context"]
request = transaction["context"]["request"]
assert request["method"] == "GET"
assert "headers" in request
headers = request["headers"]
# cookie serialization in the test client changed in Django 2.2, see
# https://code.djangoproject.com/ticket/29576
assert headers["cookie"] in (" foo=bar", "foo=bar")
env = request["env"]
assert "SERVER_NAME" in env, env.keys()
assert env["SERVER_NAME"] == "testserver"
assert "SERVER_PORT" in env, env.keys()
assert env["SERVER_PORT"] == "80"
assert "response" in transaction["context"]
response = transaction["context"]["response"]
assert response["status_code"] == 200
if "my-header" in response["headers"]:
# Django >= 2
assert response["headers"]["my-header"] == "foo"
else:
assert response["headers"]["My-Header"] == "foo"