本文整理匯總了Python中tornado.version方法的典型用法代碼示例。如果您正苦於以下問題:Python tornado.version方法的具體用法?Python tornado.version怎麽用?Python tornado.version使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tornado
的用法示例。
在下文中一共展示了tornado.version方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: set_secure_cookie
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def set_secure_cookie(self, name, value, expires_days=30, version=None,
**kwargs):
"""給cookie簽名和時間戳以防被偽造.
你必須在你的Application設置中指定 ``cookie_secret`` 來使用這個方法.
它應該是一個長的, 隨機的字節序列作為HMAC密鑰來做簽名.
使用 `get_secure_cookie()` 方法來閱讀通過這個方法設置的cookie.
注意 ``expires_days`` 參數設置cookie在瀏覽器中的有效期, 並且它是
獨立於 `get_secure_cookie` 的 ``max_age_days`` 參數的.
安全cookie(Secure cookies)可以包含任意字節的值, 而不隻是unicode
字符串(不像是普通cookie)
.. versionchanged:: 3.2.1
添加 ``version`` 參數. 提出cookie version 2
並將它作為默認設置.
"""
self.set_cookie(name, self.create_signed_value(name, value,
version=version),
expires_days=expires_days, **kwargs)
示例2: create_signed_value
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def create_signed_value(self, name, value, version=None):
"""產生用時間戳簽名的字符串, 防止被偽造.
一般通過set_secure_cookie 使用, 但對於無cookie使用來說就
作為獨立的方法來提供. 為了解碼不作為cookie存儲的值, 可以
在 get_secure_cookie 使用可選的value參數.
.. versionchanged:: 3.2.1
添加 ``version`` 參數. 提出cookie version 2
並將它作為默認設置.
"""
self.require_setting("cookie_secret", "secure cookies")
secret = self.application.settings["cookie_secret"]
key_version = None
if isinstance(secret, dict):
if self.application.settings.get("key_version") is None:
raise Exception("key_version setting must be used for secret_key dicts")
key_version = self.application.settings["key_version"]
return create_signed_value(secret, name, value, version=version,
key_version=key_version)
示例3: get_secure_cookie
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def get_secure_cookie(self, name, value=None, max_age_days=31,
min_version=None):
"""如果給定的簽名過的cookie是有效的,則返回,否則返回None.
解碼後的cookie值作為字節字符串返回(不像 `get_cookie` ).
.. versionchanged:: 3.2.1
添加 ``min_version`` 參數. 引進cookie version 2;
默認版本 1 和 2 都可以接受.
"""
self.require_setting("cookie_secret", "secure cookies")
if value is None:
value = self.get_cookie(name)
return decode_signed_value(self.application.settings["cookie_secret"],
name, value, max_age_days=max_age_days,
min_version=min_version)
示例4: _get_version
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def _get_version(value):
# Figures out what version value is. Version 1 did not include an
# explicit version field and started with arbitrary base64 data,
# which makes this tricky.
m = _signed_value_version_re.match(value)
if m is None:
version = 1
else:
try:
version = int(m.group(1))
if version > 999:
# Certain payloads from the version-less v1 format may
# be parsed as valid integers. Due to base64 padding
# restrictions, this can only happen for numbers whose
# length is a multiple of 4, so we can treat all
# numbers up to 999 as versions, and for the rest we
# fall back to v1 format.
version = 1
except ValueError:
version = 1
return version
示例5: decode_signed_value
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def decode_signed_value(secret, name, value, max_age_days=31,
clock=None, min_version=None):
if clock is None:
clock = time.time
if min_version is None:
min_version = DEFAULT_SIGNED_VALUE_MIN_VERSION
if min_version > 2:
raise ValueError("Unsupported min_version %d" % min_version)
if not value:
return None
value = utf8(value)
version = _get_version(value)
if version < min_version:
return None
if version == 1:
return _decode_signed_value_v1(secret, name, value,
max_age_days, clock)
elif version == 2:
return _decode_signed_value_v2(secret, name, value,
max_age_days, clock)
else:
return None
示例6: _decode_fields_v2
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def _decode_fields_v2(value):
def _consume_field(s):
length, _, rest = s.partition(b':')
n = int(length)
field_value = rest[:n]
# In python 3, indexing bytes returns small integers; we must
# use a slice to get a byte string as in python 2.
if rest[n:n + 1] != b'|':
raise ValueError("malformed v2 signed value field")
rest = rest[n + 1:]
return field_value, rest
rest = value[2:] # remove version number
key_version, rest = _consume_field(rest)
timestamp, rest = _consume_field(rest)
name_field, rest = _consume_field(rest)
value_field, passed_sig = _consume_field(rest)
return int(key_version), timestamp, name_field, value_field, passed_sig
示例7: _get_raw_xsrf_token
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def _get_raw_xsrf_token(self):
"""讀取或生成xsrf token 用它原本的格式.
該raw_xsrf_token是一個tuple 包含:
* version: 讀到這個token的cookie的版本,或None如果我們在該請求
中生成一個新token.
* token: 原生的token數據; 隨機(non-ascii) bytes.
* timestamp: 該token生成的時間(對於版本1的cookie將不準確)
"""
if not hasattr(self, '_raw_xsrf_token'):
cookie = self.get_cookie("_xsrf")
if cookie:
version, token, timestamp = self._decode_xsrf_token(cookie)
else:
version, token, timestamp = None, None, None
if token is None:
version = None
token = os.urandom(16)
timestamp = time.time()
self._raw_xsrf_token = (version, token, timestamp)
return self._raw_xsrf_token
示例8: create_signed_value
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def create_signed_value(
self, name: str, value: Union[str, bytes], version: int = None
) -> bytes:
"""Signs and timestamps a string so it cannot be forged.
Normally used via set_secure_cookie, but provided as a separate
method for non-cookie uses. To decode a value not stored
as a cookie use the optional value argument to get_secure_cookie.
.. versionchanged:: 3.2.1
Added the ``version`` argument. Introduced cookie version 2
and made it the default.
"""
self.require_setting("cookie_secret", "secure cookies")
secret = self.application.settings["cookie_secret"]
key_version = None
if isinstance(secret, dict):
if self.application.settings.get("key_version") is None:
raise Exception("key_version setting must be used for secret_key dicts")
key_version = self.application.settings["key_version"]
return create_signed_value(
secret, name, value, version=version, key_version=key_version
)
示例9: _get_raw_xsrf_token
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def _get_raw_xsrf_token(self) -> Tuple[Optional[int], bytes, float]:
"""Read or generate the xsrf token in its raw form.
The raw_xsrf_token is a tuple containing:
* version: the version of the cookie from which this token was read,
or None if we generated a new token in this request.
* token: the raw token data; random (non-ascii) bytes.
* timestamp: the time this token was generated (will not be accurate
for version 1 cookies)
"""
if not hasattr(self, "_raw_xsrf_token"):
cookie = self.get_cookie("_xsrf")
if cookie:
version, token, timestamp = self._decode_xsrf_token(cookie)
else:
version, token, timestamp = None, None, None
if token is None:
version = None
token = os.urandom(16)
timestamp = time.time()
assert token is not None
assert timestamp is not None
self._raw_xsrf_token = (version, token, timestamp)
return self._raw_xsrf_token
示例10: get_content_version
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def get_content_version(cls, abspath: str) -> str:
"""Returns a version string for the resource at the given path.
This class method may be overridden by subclasses. The
default implementation is a hash of the file's contents.
.. versionadded:: 3.1
"""
data = cls.get_content(abspath)
hasher = hashlib.md5()
if isinstance(data, bytes):
hasher.update(data)
else:
for chunk in data:
hasher.update(chunk)
return hasher.hexdigest()
示例11: _get_version
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def _get_version(value: bytes) -> int:
# Figures out what version value is. Version 1 did not include an
# explicit version field and started with arbitrary base64 data,
# which makes this tricky.
m = _signed_value_version_re.match(value)
if m is None:
version = 1
else:
try:
version = int(m.group(1))
if version > 999:
# Certain payloads from the version-less v1 format may
# be parsed as valid integers. Due to base64 padding
# restrictions, this can only happen for numbers whose
# length is a multiple of 4, so we can treat all
# numbers up to 999 as versions, and for the rest we
# fall back to v1 format.
version = 1
except ValueError:
version = 1
return version
示例12: _decode_fields_v2
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def _decode_fields_v2(value: bytes) -> Tuple[int, bytes, bytes, bytes, bytes]:
def _consume_field(s: bytes) -> Tuple[bytes, bytes]:
length, _, rest = s.partition(b":")
n = int(length)
field_value = rest[:n]
# In python 3, indexing bytes returns small integers; we must
# use a slice to get a byte string as in python 2.
if rest[n : n + 1] != b"|":
raise ValueError("malformed v2 signed value field")
rest = rest[n + 1 :]
return field_value, rest
rest = value[2:] # remove version number
key_version, rest = _consume_field(rest)
timestamp, rest = _consume_field(rest)
name_field, rest = _consume_field(rest)
value_field, passed_sig = _consume_field(rest)
return int(key_version), timestamp, name_field, value_field, passed_sig
示例13: clear
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def clear(self):
"""Resets all headers and content for this response."""
self._headers = httputil.HTTPHeaders({
"Server": "TornadoServer/%s" % tornado.version,
"Content-Type": "text/html; charset=UTF-8",
"Date": httputil.format_timestamp(time.time()),
})
self.set_default_headers()
if (not self.request.supports_http_1_1() and
getattr(self.request, 'connection', None) and
not self.request.connection.no_keep_alive):
conn_header = self.request.headers.get("Connection")
if conn_header and (conn_header.lower() == "keep-alive"):
self.set_header("Connection", "Keep-Alive")
self._write_buffer = []
self._status_code = 200
self._reason = httputil.responses[200]
示例14: get_content_version
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def get_content_version(cls, abspath):
"""Returns a version string for the resource at the given path.
This class method may be overridden by subclasses. The
default implementation is a hash of the file's contents.
.. versionadded:: 3.1
"""
data = cls.get_content(abspath)
hasher = hashlib.md5()
if isinstance(data, bytes_type):
hasher.update(data)
else:
for chunk in data:
hasher.update(chunk)
return hasher.hexdigest()
示例15: create_signed_value
# 需要導入模塊: import tornado [as 別名]
# 或者: from tornado import version [as 別名]
def create_signed_value(self, name, value, version=None):
"""Signs and timestamps a string so it cannot be forged.
Normally used via set_secure_cookie, but provided as a separate
method for non-cookie uses. To decode a value not stored
as a cookie use the optional value argument to get_secure_cookie.
.. versionchanged:: 3.2.1
Added the ``version`` argument. Introduced cookie version 2
and made it the default.
"""
self.require_setting("cookie_secret", "secure cookies")
secret = self.application.settings["cookie_secret"]
key_version = None
if isinstance(secret, dict):
if self.application.settings.get("key_version") is None:
raise Exception("key_version setting must be used for secret_key dicts")
key_version = self.application.settings["key_version"]
return create_signed_value(secret, name, value, version=version,
key_version=key_version)