WSGI:Python:我的功能可能不是单一传递"在尝试替换html字符时

时间:2014-09-24 10:44:57

标签: python python-2.7 mod-wsgi wsgi

问题是.. esc功能。
当我在该功能中只留下1个项目时 它实际上工作正常。

但当我有所有那些替换线..
我认为它正在进行多次通过,因此我最终得到了......

<img src="">

在网上而不是

<img src="">

...

# coding: utf-8

def esc(a):
    a = a.replace("<", "&lt;")
    a = a.replace(">", "&gt;")
    a = a.replace('"', "&quot;")
    a = a.replace("'", "&#39;")
    a = a.replace("&", "&amp;")
    return a

def application(environ, start_response):
    b = '<img src="">'
    b = esc(b)
    start_response('200 OK', [('Content-Type', 'text/html')])
    yield b

1 个答案:

答案 0 :(得分:0)

试试这个:

import html
def esc(a):
  return html.escape(a)