Python CGI脚本,我无法将'+'加运算符作为参数传递

Mic*_*ael 1 javascript python cgi url-parameters

我试图将参数传递给python cgi脚本,此参数包含加号运算符.

 /cgi-bin/test.py?input=print%20"%20"%20"%20%20-+-\"
Run Code Online (Sandbox Code Playgroud)

python cgi脚本很简单:

#!/usr/bin/python -w
import cgi
fs = cgi.FieldStorage()

print "Content-type: text/plain\n"
strE=fs.getvalue("input")
print strE
Run Code Online (Sandbox Code Playgroud)

我的输出是:

 print " " "  - -\"
Run Code Online (Sandbox Code Playgroud)

我不明白为什么'+'加运算符被空格替换,我怎么可以通过'+'加运算符?

编辑

@Tom Anderson回答了我的问题,我想再提一下我的问题.

我有一个java脚本函数调用获取带参数的url:

            <script type="text/javascript">

            function PostContentEditable(editableId,targetOutput)
            {
                        var texthtml = document.getElementById(editableId).innerHTML
                        var tmp = document.createElement("DIV");
                        tmp.innerHTML = texthtml ;
                        var str= tmp.textContent||tmp.innerText;

                        var xmlhttp;
                        if (str.length == 0){
                            document.getElementById(targetOutput).innerHTML = "";
                            return;
                        }
                        if(window.XMLHttpRequest){
                            xmlhttp=new XMLHttpRequest();
                        }
                        else{
                            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        xmlhttp.onreadystatechange=function(){
                            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                                 document.getElementById(targetOutput).innerHTML=xmlhttp.responseText;
                            }
                        }

                        xmlhttp.open("GET","../cgi-bin/exercise.py?input="+str,true);
                        xmlhttp.send();
            }
            </script>
Run Code Online (Sandbox Code Playgroud)

是否有自动内置功能,可以将所有特殊字符替换为我需要的功能?

  str = escapeStringToNativeUrl(str) ?
Run Code Online (Sandbox Code Playgroud)

Tom*_*son 5

在URL的查询部分,+是一个特殊代码,表示空格.

这是因为有关表单的HTML规范的一部分指定表单数据application/x-www-form-urlencoded在查询字符串中编码.在该编码中,空格字符被替换为"+".

因此,Python正确解码您的输入.

如果你想传递一个实际的加分,则需要百分之编码%2B.

在JavaScript中,我相信构建查询字符串的正确方法是encodeURIComponent.