[关闭]
@cyysu 2017-10-23T12:45:31.000000Z 字数 3618 阅读 804

cgi网页编写

  • 时间:2017年10月23日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
  • 版本:4.0
  • 描述:ARM上的html代码实现。

嵌入式Linux


CGI简介

  1. CGI主要的功能是在WWW环境下,藉由从客户端传递一些讯息给WWWServer,再由WWWServer去启动所指定的程式码来完成特定的工作。所以更明确的说,CGI仅是在WWWServer上可执行的程式码,而她的工作就是控制讯息要求而且产生并传回所需的文件。使用CGI,你的Server可以读取并显示在客户端无法读取的格式(像是SQLDataBase)。而且可以像闸道(Gateway)一样,在伺服端和客户端之间,产生客户端所需要的讯息。基本上,在此种主从式(Client/Server)的环境之下,其IPC(InterProcess Communication)的协定是利用讯息传递及记忆体分享(环境变数)的方式来完成。CGI有其特定的写法及规格,必须遵守其原则,方可达到主从端资讯交流的目的。

一个简单的例子

  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin
  3. i="0"
  4. echo -e "Content-type: text/html \n\n"
  5. echo "<html>"
  6. echo "<head>"
  7. echo "<title>系统状态</title>"
  8. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
  9. echo "</head>"
  10. echo "<body>"
  11. echo "<center>"
  12. echo "<h3>系统状态(5秒刷新一次)</h3>"
  13. echo "</center>"
  14. echo "<hr>"
  15. echo "<script>"
  16. echo "setTimeout(\"javascript:location.href='./system.cgi'\", 5000);"
  17. echo "</script>"
  18. echo "<center>"
  19. echo "<form>"
  20. echo "<table width=\"75%\" style=\"table-layout:fixed;\">"
  21. echo "<tr>"
  22. echo "<td>服务名</td>"
  23. echo "<td>内存使用率</td>"
  24. echo "<td>当前状态</td>"
  25. echo "</tr>"
  26. wr top -n 1 > /tmp/top.txt
  27. cat /tmp/top.txt | head -n 14 | tail -n 11 | while read line
  28. do
  29. i=`expr $i + 1`
  30. if [[ $i -eq 1 ]];then
  31. continue
  32. fi
  33. command=`echo ${line} | awk '{print $8}'`
  34. memory=`echo ${line} | awk '{print $6}'`
  35. cpu=`echo ${line} | awk '{print $7}'`
  36. echo "<tr>"
  37. echo "<td>"${command}"</td>"
  38. echo "<td>"${memory}"</td>"
  39. echo "<td>"${cpu}"</td>"
  40. echo "</tr>"
  41. done
  42. echo "</table>"
  43. echo "</form>"
  44. echo "<br>"
  45. echo "<hr>"
  46. # 显示运行程序表格
  47. echo "<h3>正在运行的应用程序</h3>"
  48. echo "<form>"
  49. echo "<table width=\"75%\" style=\"table-layout:fixed;\">"
  50. echo "<tr>"
  51. echo "<td>应用程序名称</td>"
  52. echo "<td>&nbsp;&nbsp</td>"
  53. echo "<td>当前状态</td>"
  54. echo "</tr>"
  55. # 这里查OPENCUA进程是否在运行
  56. appName=`ps -a | grep -i OPEN | grep -v grep | cut -d " " -f7`
  57. if [ "$appName" = "" ];then
  58. status="挂起"
  59. appName="./OPUCA"
  60. echo "<tr>"
  61. echo "<td>"${appName}"</td>"
  62. echo "<td></td>"
  63. echo "<td><font color="red">"${status}"</font></td>"
  64. echo "</tr>"
  65. else
  66. status="运行"
  67. echo "<tr>"
  68. echo "<td>"${appName}"</td>"
  69. echo "<td></td>"
  70. echo "<td><font color="green">"${status}"</font></td>"
  71. echo "</tr>"
  72. fi
  73. # 这里查NEWTON进程是否在运行
  74. appName=`ps -a | grep -i NEWTON | grep -v grep | cut -d " " -f7`
  75. if [ "$appName" = "" ];then
  76. appName="./NEWTON"
  77. status="挂起"
  78. echo "<tr>"
  79. echo "<td>"${appName}"</td>"
  80. echo "<td></td>"
  81. echo "<td><font color="red">"${status}"</font></td>"
  82. echo "</tr>"
  83. else
  84. status="运行"
  85. echo "<tr>"
  86. echo "<td>"${appName}"</td>"
  87. echo "<td></td>"
  88. echo "<td><font color=\"green\">"${status}"</font></td>"
  89. echo "</tr>"
  90. fi
  91. # 这里查MODBUS进程是否在运行
  92. appName=`ps -a | grep -i MODBUS | grep -v grep | cut -d " " -f7`
  93. if [ "$appName" = "" ];then
  94. status="挂起"
  95. appName="./MODBUS"
  96. echo "<tr>"
  97. echo "<td>"${appName}"</td>"
  98. echo "<td></td>"
  99. echo "<td><font color="red">"${status}"</font></td>"
  100. echo "</tr>"
  101. else
  102. status="运行"
  103. echo "<tr>"
  104. echo "<td>"${appName}"</td>"
  105. echo "<td></td>"
  106. echo "<td><font color="green">"${status}"</font></td>"
  107. echo "</tr>"
  108. fi
  109. # 这里查MQTT进程是否在运行
  110. appName=`ps -a | grep -i MQTT | grep -v grep | cut -d " " -f7`
  111. if [ "$appName" = "" ];then
  112. status="挂起"
  113. appName="./MQTT"
  114. echo "<tr>"
  115. echo "<td>"${appName}"</td>"
  116. echo "<td></td>"
  117. echo "<td><font color="red">"${status}"</font></td>"
  118. echo "</tr>"
  119. else
  120. status="运行"
  121. echo "<tr>"
  122. echo "<td>"${appName}"</td>"
  123. echo "<td></td>"
  124. echo "<td><font color="green">"${status}"</font></td>"
  125. echo "</tr>"
  126. fi
  127. echo "</table>"
  128. echo "</form>"
  129. echo "</center>"
  130. echo "</body>"
  131. echo -e "</html>\n\n"

我这里举的例子是ARM板的web控制终端。我们这里采用的是shell方式去编写的程序,当然我们还可以采用其他脚本编写,比如C或者python,在比如perl等等。通过观察上面的例子我们可以发现这不就是在编写脚本嘛,对没错,其实这就是编写脚本,只不过在浏览器那边访问的时候这些代码被解析成了html代码,这样就可以看到我们网页了,如果这些程序在没有启动http服务器的那么浏览器访问的就是这些文件的内容。

由于PC上有很多http服务器了,这里我给出一个arm端的http服务器。

ARM端小型Http服务端

这里说明一下使用方法:
将这个程序放入你的网站根目录,程序会默认寻找你根目录下的index.html去解析,同时他还支持加入h5或者css3等网页解析。程序默认开启的端口是8080。下面给出一个效果图。

  1. [root@3352-T web]# ls
  2. banner.html css/ img/ logo.html test.html webs*
  3. bottom.html graphics/ index.html main.html tree.html
  4. cgi-bin/ httpd* js/ menu.html um/

web控制端

这里给一个演示动画。

web控制端演示动画

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注