[OK210開發(fā)板體驗]系統(tǒng)篇(3) 基于OK210的智能家居系統(tǒng)之后端設計CGI
CGI通信方式,主要介紹CGI對應表單Get和Post方法的數(shù)據(jù)交互;
CGI簡單示例,通過簡單的post和get方法,加深對CGI接口通信的理解
一、CGI工作原理
二、 CGI通信方式
三、 CGI簡單示例
(1) printf("Content-Type:text/html/n/n");
此行通過標準輸出將字符串″Contenttype:text/plain/n/n″傳送給Web服務器。它是一個MIME頭信息,它告訴Web服務器隨 后的輸出是以純ASCII文本的形式。請注意在這個頭信息中有兩個換行符,這是因為Web服務器需要在實際的文本信息開始之前先看見一個空行。
(2) data = getenv("QUERY_STRING");
CGI定義:當GET方法提交的表單被發(fā)送到服務器斷后,表單中的數(shù)據(jù)被保存在服務器上一個叫做QUERY_STRING的環(huán)境變量中。這種表單的處理相對簡單,只要讀取環(huán)境變量就可以了。
(3) sscanf(data,"a=%[^&]&b=%s",a,b)!=2
這個是關(guān)于sscanf函數(shù)的使用問題,自己可以上網(wǎng)搜索一下,這里不再詳述!
(4)atoi(a)+atoi(b)
atoi函數(shù)的功能是將字符型成整型,只有轉(zhuǎn)換之后才可以進行加法運算!
(5) lenstr=getenv("CONTENT_LENGTH");
Web服務器在調(diào)用使用POST方法的CGI程序時設置此環(huán)境變量,它的文本值表示Web服務器傳送給CGI程序的輸入中的字符數(shù)目,因此需要使用函數(shù)atoi() 將此環(huán)境變量的值轉(zhuǎn)換成整數(shù),并賦給變量len(下面有定義)。
(6) fgets(poststr,len+1,stdin);
這個是關(guān)于fgets函數(shù)的使用問題,自己可以上網(wǎng)搜索一下,這里不再詳述!
- 
					//get.c
 - 
					#include <stdio.h>
 - 
					#include <stdlib.h>
 - 
					int main(void)
 - 
					{
 - 
					        char *data;
 - 
					        char a[10],b[10];
 - 
					        printf("Content-Type:text/html\n\n");
 - 
					        printf("<HTML>\n");
 - 
					        printf("<HEAD>\n<TITLE >Get Method</TITLE>\n</HEAD>\n");
 - 
					        printf("<BODY>\n");
 - 
					        printf("<div style=\"font-size:12px\">\n");
 - 
					        data = getenv("QUERY_STRING");
 - 
					        if(sscanf(data,"a=%[^&]&b=%s",a,b)!=2){
 - 
					                printf("<DIV STYLE=\"COLOR:RED\">Error parameters should be entered!</DIV>\n");
 - 
					        }
 - 
					        else{
 - 
					               printf("<DIV STYLE=\"COLOR:GREEN; font-size:15px;font-weight:bold\">a + b = %d</DIV>\n",atoi(a)+atoi(b));
 - 
					        }
 - 
					        printf("<HR COLOR=\"blue\" align=\"left/" width=\"100\">");
 - 
					        printf("<input type=\"button\" value=\"Back CGI/" onclick=\"javascript:window.location='../cgi.html'/">");
 - 
					        printf("
 - 
					\n");
 - 
					        printf("</BODY>\n");
 - 
					        printf("</HTML>\n");
 - 
					        return 0;
 - 
					}
 
- 
					//post.c
 - 
					#include <stdio.h>
 - 
					#include <stdlib.h>
 - 
					int main(void){
 - 
					        int len;
 - 
					        char *lenstr,poststr[20];
 - 
					        char m[10],n[10];
 - 
					        printf("Content-Type:text/html\n\n");
 - 
					        printf("<HTML>\n");
 - 
					        printf("<HEAD>\n<TITLE >post Method</TITLE>\n</HEAD>\n");
 - 
					        printf("<BODY>/n");
 - 
					        printf("<div style= \"font-size:12px\">\n");
 - 
					        lenstr=getenv("CONTENT_LENGTH");
 - 
					        if(lenstr == NULL)
 - 
					                printf("<DIV STYLE=\"COLOR:RED\">Error parameters should be entered!</DIV>\n");
 - 
					        else{
 - 
					                len=atoi(lenstr);
 - 
					                fgets(poststr,len+1,stdin);
 - 
					                if(sscanf(poststr,"m=%[^&]&n=%s",m,n)!=2){
 - 
					                        printf("<DIV STYLE=\"COLOR:RED\">Error: Parameters are not right!</DIV>\n");
 - 
					                }
 - 
					                else{
 - 
					                       printf("<DIV STYLE=\"COLOR:GREEN; font-size:15px;font-weight:bold\">m * n = %d</DIV>\n",atoi(m)*atoi(n));
 - 
					                }
 - 
					        }
 - 
					        printf("<HR COLOR=\"blue\" align=\"left\" width=\"100\">");
 - 
					        printf("<input type=\"button\" value=\"Back CGI\" onclick=\"javascript:window.location='../cgi.html'\">");
 - 
					        printf("\n");
 - 
					        printf("</BODY>\n");
 - 
					        printf("</HTML>\n");
 - 
					        fflush(stdout);
 - 
					        return 0;
 - }
 
- 
					<!--
 - 
					cgi.html
 - 
					-->
 - 
					<html>
 - 
					<head>
 - 
					<title>CGI Testing</title>
 - 
					</head>
 - 
					<body>
 - 
					<table width="200" height="180" border="0" style="font-size:12px">
 - 
					<tr><td>
 - 
					<div style="font-weight:bold; font-size:15px">Method: GET
 - 
					<div>please input two number:<div>
 - 
					<form method="get" action="./cgi-bin/get">
 - 
					<input type="txt" size="3" name="a">+
 - 
					<input type="txt" size="3" name="b">=
 - 
					<input type="submit" value="sum">
 - 
					</form>
 - 
					</td></tr>
 - 
					<tr><td>
 - 
					<div style="font-weight:bold; font-size:15px">Method: POST
 - 
					<div>please input two number:<div>
 - 
					<form method="post" action="./cgi-bin/post">
 - 
					<input type="txt" size="3" name="m">*
 - 
					<input type="txt" size="3" name="n">=
 - 
					<input type="submit" value="resu">
 - 
					</form>
 - 
					</td></tr>
 - 
					<tr><td><inputtype="button" value="Back Home"onclick='javascript:window.location="./index.html"'></td></tr>
 - 
					</table>
 - 
					</body>
 - 
					</html>
 
相關(guān)產(chǎn)品 >
- 
                
OKMX6UL-C開發(fā)板
飛凌嵌入式專注imx6系列imx6ul開發(fā)板、飛思卡爾imx6ul核心板等ARM嵌入式核心控制系統(tǒng)研發(fā)、設計和生產(chǎn),i.mx6UL系列產(chǎn)品現(xiàn)已暢銷全國,作為恩智浦imx6ul,imx6ul開發(fā)板,i.mx6提供者,飛凌嵌入式提供基于iMX6 iMX6UL解決方案定制。
了解詳情
 - 
                
OKMX6ULL-C開發(fā)板
40*29mm,雙網(wǎng)雙CAN,8路串口| i.MX6ULL開發(fā)板是基于NXP i.MX6ULL設計開發(fā)的的一款Linux開發(fā)板 ,主頻800MHz,體積小,其核心板僅40*29mm,采用板對板連接器,適應場景豐富。 了解詳情
 

