求教:用C语言写CGI,如果接收用POST方法提交

Python011

求教:用C语言写CGI,如果接收用POST方法提交,第1张

服务器 的 CGI 程序 一次调用,只服务于 一个 客户,与别的客户无关。

多客户 向 服务器 申请服务, 服务器 有自己 的 服务方法的 安排 (排队,等待,服务)。

能同时 服务 多少 客户 (即 跑几个线程)等等,服务器 启动前 配置文件里 需 设好。

POST或GET,涉及到 同一数据文件 读写更新 时,需要设 文件 锁定 和 开启。(多线程程序常用)。

放哪里都可以,进到文件所放路径,用gcc编译器编译。如:gcc -o test test.c 就是说把test.c文件编译链接生成test可执行程序。然后再命令:./test linux下“。/”表示运行

没用过boa。 只谈网页原理。

从data.txt读入温度,湿度等数据。

用printf()输出 HTML 文件。

#define LF 10

#define CR 13

int wendu,shidu

char shijian[20]

FILE *fin

// 打开data.txt,读入 wendu,shidu,shijian,关文件

// 下来输出

printf("<HTML>%c",LF)

printf("<HEAD><TITLE>web界面</TITLE></HEAD>%c",LF)

printf("<meta http-equiv=\"Content-Type\"

content=\"text/html\"charset=\"gb2312\" />)

printf("<BODY>%c",LF)

printf("<H2>温度:%d 湿度:%d 时间:%s</H2>%c",LF,wendu,shidu,shijian)

printf("<HR></BODY></HTML>%c",LF)

===============================================

网上留言获取,变量名字与网页定义有关。

char *cl

char manydata[10240]

void getword(char *word, char *line, char stop) {

int x = 0,y

for(x=0((line[x]) &&(line[x] != stop))x++)

word[x] = line[x]

word[x] = '\0'

if(line[x]) ++x

y=0

while(line[y++] = line[x++])

}

void unescape_url(char *url) {

register int x,y

for(x=0,y=0url[y]++x,++y) {

if((url[x] = url[y]) == '%') {

url[x] = x2c(&url[y+1])

y+=2

}

}

url[x] = '\0'

}

void plustospace(char *str) {

register int x

for(x=0str[x]x++) if(str[x] == '+') str[x] = ' '

}

返回信息长度:

char_l = atoi(getenv("CONTENT_LENGTH"))

cl = &manydata[0]

分离变量名与值(即内容):

for (i=0i <char_l i++) {

*( cl + i) = (char)fgetc(stdin)

}

*( cl + char_l) = '\0'

查找需要的变量名与值:

for(x=0cl[0] != '\0'x++) {

m=x

getword(w,cl,'=')

plustospace(w)

unescape_url(w)

if( strcmp(w,"name") == 0) {

getword(w,cl,'&')

plustospace(w)

unescape_url(w)

strcpy(name,w)

}

。。。。

}

---------------------------------