linux下shell处理cgi的方法--post get
2014-08-21 17:12
393 查看
web server获取网页上HTML的DOM对象信息发送来的信息,主要有get和post方法。
介绍原理的文章很多,但介绍实际应用到linux的shell中的文章很少。
1、get方法
①表单源码如下,有几个HTML对象作为传值对象。
<html>
<head>
<title>CGI Test</title>
</head>
<body>
<form name="form1" method="get" action="/cgi-bin/test-get">
<p>Text field
<input name="Text_Field" type="text" id="Text_Field">
</p>
<p>Radio button </p>
<p>
<input name="Radio_Button" type="radio" value="1"> 1
</p>
<p>
<input name="Radio_Button" type="radio" value="2"> 2
</p>
<p>
<input name="Radio_Button" type="radio" value="3"> 3
</p>
<p> </p>
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</p>
<p> </p>
<p> </p>
</form>
</body>
</html>
②cgi get脚本,使用环境变量QUERY_STRING传值。
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "<HTML><HEAD><TITLE>Sample CGI Output</TITLE></HEAD>"
echo "<BODY>"
echo "<pre>"
#echo "Environment variables"
#echo ""
env
#echo ""
#echo "========================================================="
#echo ""
#echo "Form variables :"
#echo ""
str=$QUERY_STRING
echo $str
# [ "${str%&}" = "$str" ] && str="$str&"
# pair=${str%%&*}
# var=${pair%=*}
# val=${pair#*=}
pair=${str##*&}
var=${pair%=*}
val=${pair#*=}
str=${str#*&}
echo "$pair"
echo "$var"
echo "$val"
echo "$str"
echo ""
#echo $val
if [ $val = "lighton" ];then
echo "select to open light"
fi
if [ $val = "lightoff" ];then
echo "select to close light"
fi
if [ $val = "rolling" ];then
echo "select to rolling light"
fi
echo "</pre>"
echo "</BODY>"
echo "</HTML>"
2、post方法
①表单源码
<html>
<head>
<title>CGI Test</title>
</head>
<body>
<body style="text-align: center">
<form name="form1" method="post" action="/cgi-bin/test-post">
<p>Text field
<input name="Text_Field" type="text" id="Text_Field">
</p>
<p>Radio button </p>
<p>
<input name="Radio_Button" type="radio" value="1"> 1
</p>
<p>
<input name="Radio_Button" type="radio" value="2"> 2
</p>
<p>
<input name="Radio_Button" type="radio" value="3"> 3
</p>
<p>Some text </p>
<p>
<textarea name="Text_Area" id="Text_Area"></textarea>
</p>
<p> </p>
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</p>
<p> </p>
<p> </p>
</form>
</body>
</html>
②post传值时,使用环境变量CONTENT_LENGTH表示传输数据长度。
而实际内容需要从标准输入设备接收,这里使用命令“read pos -n CONTENT_LENGTH”,注意,接收个数需做限制。
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "<HTML><HEAD><TITLE>Sample CGI Output</TITLE></HEAD>"
echo "<BODY>"
echo "<pre>"
echo "Environment variables"
echo ""
env
echo ""
echo "========================================================="
echo ""
echo "Form variables :"
echo ""
#echo $CONTENT_LENGTH
read pos -n CONTENT_LENGTH
#echo $pos
tmp=${pos%%&*}
#echo $tmp
tmp=${tmp#*=}
echo "Text_Field=$tmp"
tmpcur=${pos#*&}
#echo $tmpcur
tmp=${tmpcur%%&*}
tmp=${tmp#*=}
echo "Radio_Button=$tmp"
tmpcur=${tmpcur#*&}
tmp=${tmpcur%%&*}
tmp=${tmp#*=}
echo "Text_Area=$tmp"
echo "</pre>"
echo "</BODY></HTML>"
本文参考http://blog.chinaunix.net/uid-21025382-id-168762.html,非常感谢,但原作对于post的处理存在错误之处,这里纠正出来。
介绍原理的文章很多,但介绍实际应用到linux的shell中的文章很少。
1、get方法
①表单源码如下,有几个HTML对象作为传值对象。
<html>
<head>
<title>CGI Test</title>
</head>
<body>
<form name="form1" method="get" action="/cgi-bin/test-get">
<p>Text field
<input name="Text_Field" type="text" id="Text_Field">
</p>
<p>Radio button </p>
<p>
<input name="Radio_Button" type="radio" value="1"> 1
</p>
<p>
<input name="Radio_Button" type="radio" value="2"> 2
</p>
<p>
<input name="Radio_Button" type="radio" value="3"> 3
</p>
<p> </p>
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</p>
<p> </p>
<p> </p>
</form>
</body>
</html>
②cgi get脚本,使用环境变量QUERY_STRING传值。
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "<HTML><HEAD><TITLE>Sample CGI Output</TITLE></HEAD>"
echo "<BODY>"
echo "<pre>"
#echo "Environment variables"
#echo ""
env
#echo ""
#echo "========================================================="
#echo ""
#echo "Form variables :"
#echo ""
str=$QUERY_STRING
echo $str
# [ "${str%&}" = "$str" ] && str="$str&"
# pair=${str%%&*}
# var=${pair%=*}
# val=${pair#*=}
pair=${str##*&}
var=${pair%=*}
val=${pair#*=}
str=${str#*&}
echo "$pair"
echo "$var"
echo "$val"
echo "$str"
echo ""
#echo $val
if [ $val = "lighton" ];then
echo "select to open light"
fi
if [ $val = "lightoff" ];then
echo "select to close light"
fi
if [ $val = "rolling" ];then
echo "select to rolling light"
fi
echo "</pre>"
echo "</BODY>"
echo "</HTML>"
2、post方法
①表单源码
<html>
<head>
<title>CGI Test</title>
</head>
<body>
<body style="text-align: center">
<form name="form1" method="post" action="/cgi-bin/test-post">
<p>Text field
<input name="Text_Field" type="text" id="Text_Field">
</p>
<p>Radio button </p>
<p>
<input name="Radio_Button" type="radio" value="1"> 1
</p>
<p>
<input name="Radio_Button" type="radio" value="2"> 2
</p>
<p>
<input name="Radio_Button" type="radio" value="3"> 3
</p>
<p>Some text </p>
<p>
<textarea name="Text_Area" id="Text_Area"></textarea>
</p>
<p> </p>
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</p>
<p> </p>
<p> </p>
</form>
</body>
</html>
②post传值时,使用环境变量CONTENT_LENGTH表示传输数据长度。
而实际内容需要从标准输入设备接收,这里使用命令“read pos -n CONTENT_LENGTH”,注意,接收个数需做限制。
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "<HTML><HEAD><TITLE>Sample CGI Output</TITLE></HEAD>"
echo "<BODY>"
echo "<pre>"
echo "Environment variables"
echo ""
env
echo ""
echo "========================================================="
echo ""
echo "Form variables :"
echo ""
#echo $CONTENT_LENGTH
read pos -n CONTENT_LENGTH
#echo $pos
tmp=${pos%%&*}
#echo $tmp
tmp=${tmp#*=}
echo "Text_Field=$tmp"
tmpcur=${pos#*&}
#echo $tmpcur
tmp=${tmpcur%%&*}
tmp=${tmp#*=}
echo "Radio_Button=$tmp"
tmpcur=${tmpcur#*&}
tmp=${tmpcur%%&*}
tmp=${tmp#*=}
echo "Text_Area=$tmp"
echo "</pre>"
echo "</BODY></HTML>"
本文参考http://blog.chinaunix.net/uid-21025382-id-168762.html,非常感谢,但原作对于post的处理存在错误之处,这里纠正出来。
相关文章推荐
- 在CGI程序中POST和GET消息的处理
- PHP处理网页表单GET和POST方法
- Linux实用技巧-CURL实现HTTP的GET POST方法
- linux命令发送http的get或post请求(curl和wget两种方法)
- .net调用别人的http页面,返回结果,url中有中文的处理,get和post两种方法
- xhr的send方法以及node如何处理get和post数据
- CGI程序中POST和GET消息的处理
- 使用POST表单处理方法,编写cgi和html文件,能够实现中文显示
- HttpURLConnection的get和post处理方法(含图片)
- HttpURLConnection的get和post处理方法(含图片)
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- C语言CGI程序--POST表单/GET表单处理
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- linux 的shell处理两Excel的比较方法
- 第二百五十八节,Tornado框架-逻辑处理get()方法和post()方法,初识模板语言
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- 用c语言写cgi程序(4)---处理请求post get。文本框,下拉列表框。
- 用c语言写cgi程序(4)---处理请求post get。文本框,下拉列表框。