tornado.escape — 跳脫和字串操作

HTML、JSON、URL 和其他內容的跳脫/取消跳脫方法。

也包括一些隨著時間推移而加入的其他雜項字串操作函式。

此模組中的許多函式在標準程式庫中都有近似的對應項(差異主要與位元組和 Unicode 字串的處理有關,在 Python 2 中更為相關)。在新程式碼中,建議在適用的情況下使用標準程式庫函式,而不是此模組。請參閱每個函式的 docstring 以了解詳細資訊。

跳脫函式

tornado.escape.xhtml_escape(value: Union[str, bytes]) str[原始碼]

跳脫字串,使其在 HTML 或 XML 中有效。

跳脫字元 <>"'&。在屬性值中使用時,跳脫的字串必須用引號括起來。

等效於 html.escape,但此函式始終傳回 str 類型,而 html.escape 如果其輸入為 bytes,則傳回 bytes

在 3.2 版本中變更:將單引號新增至跳脫字元清單中。

在 6.4 版本中變更:現在簡單地包裝 html.escape。這等效於舊的行為,只是單引號現在被跳脫為 &#x27; 而不是 &#39;,且效能可能有所不同。

tornado.escape.xhtml_unescape(value: Union[str, bytes]) str[原始碼]

取消跳脫 XML 跳脫的字串。

等效於 html.unescape,但此函式始終傳回 str 類型,而 html.unescape 如果其輸入為 bytes,則傳回 bytes

在 6.4 版本中變更:現在簡單地包裝 html.unescape。這會根據 HTML 5 規格 https://html.spec.whatwg.org/multipage/parsing.html#numeric-character-reference-end-state 的要求,變更某些輸入的行為。

現在,某些無效輸入(例如 surrogate)會引發錯誤,並且現在可以正確處理對某些 ISO-8859-1 字元的數值參照。

tornado.escape.url_escape(value: Union[str, bytes], plus: bool = True) str[原始碼]

傳回指定值的 URL 編碼版本。

等效於 urllib.parse.quote_plusurllib.parse.quote,取決於 plus 引數。

如果 plus 為 true(預設值),則空格將表示為 +,而斜線將表示為 %2F。這適用於查詢字串。如果 plus 為 false,則空格將表示為 %20,而斜線保持原樣。這適用於 URL 的路徑元件。請注意,預設值 plus=True 實際上是 Python 的 urllib 模組的反向。

在 3.1 版本中新增:plus 引數

tornado.escape.url_unescape(value: Union[str, bytes], encoding: None, plus: bool = True) bytes[原始碼]
tornado.escape.url_unescape(value: Union[str, bytes], encoding: str = 'utf-8', plus: bool = True) str

將給定的值從 URL 解碼。

參數可以是位元組字串或 Unicode 字串。

如果 encoding 為 None,則結果將為位元組字串,且如果 plus=False,此函式等同於 urllib.parse.unquote_to_bytes。否則,結果將為指定編碼的 Unicode 字串,且此函式等同於 urllib.parse.unquote_plusurllib.parse.unquote,但此函式也接受 bytes 作為輸入。

如果 plus 為 true (預設值),則加號將被解譯為空格 (字面上的加號必須以「%2B」表示)。這適用於查詢字串和表單編碼值,但不適用於 URL 的路徑元件。請注意,此預設值與 Python 的 urllib 模組相反。

在 3.1 版本中新增:plus 引數

tornado.escape.json_encode(value: Any) str[原始碼]

將給定的 Python 物件編碼為 JSON。

等同於 json.dumps,並額外保證輸出永遠不會包含字元序列 </,當 JSON 嵌入 HTML <script> 標籤時,這可能會造成問題。

tornado.escape.json_decode(value: Union[str, bytes]) Any[原始碼]

傳回給定 JSON 字串的 Python 物件。

支援 strbytes 輸入。等同於 json.loads

位元組/Unicode 轉換

tornado.escape.utf8(value: bytes) bytes[原始碼]
tornado.escape.utf8(value: str) bytes
tornado.escape.utf8(value: None) None

將字串引數轉換為位元組字串。

如果引數已經是位元組字串或 None,則會保持不變並傳回。否則,它必須是 Unicode 字串,並編碼為 utf8。

tornado.escape.to_unicode(value: str) str[原始碼]
tornado.escape.to_unicode(value: bytes) str
tornado.escape.to_unicode(value: None) None

將字串引數轉換為 Unicode 字串。

如果引數已經是 Unicode 字串或 None,則會保持不變並傳回。否則,它必須是位元組字串,並解碼為 utf8。

tornado.escape.native_str()
tornado.escape.to_basestring()

將位元組或 unicode 字串轉換為 str 類型。這些函式過去用於協助從 Python 2 過渡到 Python 3,但現在已被棄用,成為 to_unicode 的別名。

tornado.escape.recursive_unicode(obj: Any) Any[原始碼]

走訪簡單的資料結構,將位元組字串轉換為 unicode。

支援列表、元組和字典。

其他函式

tornado.escape.linkify(text: Union[str, bytes], shorten: bool = False, extra_params: Union[str, Callable[[str], str]] = '', require_protocol: bool = False, permitted_protocols: List[str] = ['http', 'https']) str[原始碼]

將純文字轉換為含有連結的 HTML。

例如:linkify("Hello http://tornadoweb.org!") 將會回傳 Hello <a href="http://tornadoweb.org">http://tornadoweb.org</a>!

參數

  • shorten:長網址將會縮短以供顯示。

  • extra_params:要包含在連結標籤中的額外文字,或是一個可呼叫的物件,接受連結作為參數並回傳額外文字,例如 linkify(text, extra_params='rel="nofollow" class="external"'),或是

    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:僅連結包含協定的網址。如果此值為 False,則像 www.facebook.com 這樣的網址也會被連結化。

  • permitted_protocols:應該被連結化的協定列表(或集合),例如 linkify(text, permitted_protocols=["http", "ftp", "mailto"])。包含像 javascript 這樣的協定是非常不安全的。

tornado.escape.squeeze(value: str) str[原始碼]

將所有連續的空白字元替換為單一空格。