python - FieldStorage 输入删除了一些字符

标签 python python-2.7 cgi

将“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">&nbsp;
    <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/

相关文章:

python - 将列表 A 的每个元素与列表 B 的每个元素压缩 - 最佳方法 "pythonie"

python - 属性错误 : 'module' object has no attribute 'tests'

c++ - 在 apache2 上使用 cgi c++ 处理上传的文件

php - Nginx + php7.0-fpm = 空白页面

python-2.7 - 使用Docker Remote API运行exec时出现错误500

python - “模块”对象没有属性 'FieldStorage'

python - s3cmd 与 python 中的 boto 同步

python - 我如何过滤 itertools chain() 结果?

python - Pytorch 在 __init__() 中定义层和直接在 forward() 中使用有什么区别?

python - Windows 上没有适用于 Python 3.5 的 cx_Oracle 吗?