[英]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.