简体   繁体   English

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

[英]Python CGI script, i am not able to pass '+' plus operator as a parameter

I am trying to pass a parameter to a python cgi script, this parameter contains the plus operator. 我试图将参数传递给python cgi脚本,此参数包含加号运算符。

 /cgi-bin/test.py?input=print%20"%20"%20"%20%20-+-\"

The python cgi script is simple: python cgi脚本很简单:

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

print "Content-type: text/plain\n"
strE=fs.getvalue("input")
print strE

my output is: 我的输出是:

 print " " "  - -\"

I don't understand why the '+' plus operator is substituted by space, How may i pass the '+' plus operator? 我不明白为什么'+'加号用空格代替,我如何传递'+'加号?

EDIT 编辑

@Tom Anderson, answered my question, and i want to extend my question a bit more. @汤姆·安德森(Tom Anderson),回答了我的问题,我想再扩展我的问题。

I have a java-script function that invokes gets the url with the parameters: 我有一个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>

Is there an automatic built-in function that replaces all special characters to what i need? 是否有自动内置的功能来替换所有需要的特殊字符?

  str = escapeStringToNativeUrl(str) ?

In the query part of the URL, + is a special code that means a space. 在URL的查询部分中, +是特殊代码,表示空格。

This is because the part of the HTML specification concerning forms specifies that form data is encoded as application/x-www-form-urlencoded in a query string . 这是因为HTML规范中有关表单的部分指定表单数据在查询字符串中被编码为application/x-www-form-urlencoded In that encoding, space characters are replaced by `+' . 在那种编码中, 空格字符被`+'代替

So, Python is correctly decoding your input. 因此,Python正确解码了您的输入。

If you want to pass an actual plus, you will need to percent-encode it as %2B . 如果要传递实际的加号,则需要将其百分比编码%2B

In JavaScript, i believe the right way to build the query string is with encodeURIComponent . 在JavaScript中,我相信构建查询字符串的正确方法是使用encodeURIComponent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用CGI方法通过Web服务器将参数从JavaScript传递到Perl脚本 - Using CGI methods to pass a parameter from JavaScript to Perl script over a web server 如何使用JavaScript从CGI脚本调用python函数? - How do I call a python function from a cgi script with javascript? 我可以将数据从Spring引导控制器传递到html页面,但是如何使用Java Script访问数据? - I am able to pass the data from Spring boot controller to html page, but how do I access the data in Java Script? 无法将参数传递给 function - Not able to pass parameter to function 为什么我无法使用扩展运算符输入参数? - Why am I losing typing of a parameter with spread operator? 我无法使用jQuery将html文本框值传递给webservice - i am not able to pass the html textbox values to webservice using jquery 为什么我无法将纬度和经度传递给JavaScript文件? - Why am I not able to pass latitude and longtitude to a JavaScript file? 我无法在vue js中传递lat和lng的值? - I am not able to pass values of lat and lng in vue js? 我能将class属性传递给angularJS中的指令模板吗? - Am I able to pass a class attribute to a directive template in angularJS? 无法在脚本中传递值 - Not able to pass value in script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM