tornado.escape — Escaping and string manipulation¶
Escaping/unescaping methods for HTML, JSON, URLs, and others.
Also includes a few other miscellaneous string manipulation functions thathave crept in over time.
Escaping functions¶
tornado.escape.
xhtmlescape
(_value)[源代码]¶
Escapes a string so it is valid within HTML or XML.
Escapes the characters<
,>
,"
,'
, and&
.When used in attribute values the escaped strings must be enclosedin quotes.
在 3.2 版更改: Added the single quote to the list of escaped characters.
tornado.escape.
urlescape
(_value, plus=True)[源代码]¶
Returns a URL-encoded version of the given value.
Ifplus
is true (the default), spaces will be representedas “+” instead of “%20”. This is appropriate for query stringsbut not for the path component of a URL. Note that this defaultis the reverse of Python’s urllib module.
3.1 新版功能: Theplus
argument
tornado.escape.
urlunescape
(_value, encoding='utf-8', plus=True)[源代码]¶
Decodes the given value from a URL.
The argument may be either a byte or unicode string.
If encoding is None, the result will be a byte string. Otherwise,the result is a unicode string in the specified encoding.
Ifplus
is true (the default), plus signs will be interpretedas spaces (literal plus signs must be represented as “%2B”). Thisis appropriate for query strings and form-encoded values but notfor the path component of a URL. Note that this default is thereverse of Python’s urllib module.
3.1 新版功能: Theplus
argument
Byte/unicode conversions¶
These functions are used extensively within Tornado itself,but should not be directly needed by most applications. Note thatmuch of the complexity of these functions comes from the fact thatTornado supports both Python 2 and Python 3.
tornado.escape.
utf8
(value)[源代码]¶
Converts a string argument to a byte string.
If the argument is already a byte string or None, it is returned unchanged.Otherwise it must be a unicode string and is encoded as utf8.
tornado.escape.
tounicode
(_value)[源代码]¶
Converts a string argument to a unicode string.
If the argument is already a unicode string or None, it is returnedunchanged. Otherwise it must be a byte string and is decoded as utf8.
tornado.escape.
nativestr
()¶
Converts a byte or unicode string into typestr
. Equivalent toutf8
on Python 2 andto_unicode
on Python 3.
tornado.escape.
to_basestring
(_value)[源代码]¶
Converts a string argument to a subclass of basestring.
In python2, byte and unicode strings are mostly interchangeable,so functions that deal with a user-supplied argument in combinationwith ascii string constants can use either and should return the typethe user supplied. In python3, the two types are not interchangeable,so this method is needed to convert byte strings to unicode.
tornado.escape.
recursiveunicode
(_obj)[源代码]¶
Walks a simple data structure, converting byte strings to unicode.
Supports lists, tuples, and dictionaries.
Miscellaneous functions¶
tornado.escape.
linkify
(text, shorten=False, extra_params='', require_protocol=False, permitted_protocols=['http', 'https'])[源代码]¶
Converts plain text into HTML with links.
For example:linkify("Hello http://tornadoweb.org!")
would returnHello <a href="http://tornadoweb.org">http://tornadoweb.org</a>!
Parameters:
-
shorten: Long urls will be shortened for display.
- extraparams: Extra text to include in the link tag, or a callable
taking the link as an argument and returning the extra texte.g. linkify(text, extra_params='rel="nofollow" class="external"'),or:
def extra_params_cb(url):
if url.startswith("http://example.com"):
return 'class="internal"'
else:
return 'class="external" rel="nofollow"'
linkify(text, extra_params=extra_params_cb)
- require_protocol: Only linkify urls which include a protocol. If
this is False, urls such as www.facebook.com will also be linkified.
- permitted_protocols: List (or set) of protocols which should be
linkified, e.g. linkify(text, permitted_protocols=["http", "ftp",
"mailto"]). It is very unsafe to include protocols such asjavascript.
原文:
https://tornado-zh-cn.readthedocs.io/zh_CN/latest/escape.html