将“c++”放入输入框中,我的 Python 脚本只收到“c”。
这是 HTML 代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<input id="inputtxt" type="text">
<a onclick="window.location='escapetest.py?q='+document.getElementById('inputtxt').value;">Go!</a>
</body>
</html>
以及接收请求的python脚本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cgitb; cgitb.enable()
from cgi import FieldStorage
inputstr = FieldStorage()["q"]
print "Content-Type: text/html; charset=utf-8"
print
print inputstr.value
输出为:
c
运行 Python 2.7 (x64) 并使用 Firefox。
最佳答案
你没有正确地逃避你的值(value); URL-encoded query value 中的 +
字符是空格的编码值,所以你实际上正在打印:
"c "
一个带有两个空格的c
。使用encodeURIComponent()
function正确转义输入值,其中空格将被 +
替换,+
将被 %2B
替换,以便 Python 可以解码该值返回+
:
<a onclick="window.location='escapetest.py?q='+encodeURIComponent(document.getElementById('inputtxt').value);">Go!</a>
关于python - FieldStorage 输入删除了一些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28707940/