简体   繁体   English

bash命令的CGI脚本

[英]CGI scripts for bash commands

I'm kinda new to all this, so tell me if I'm doing something totally wrong. 我有点陌生,所以请告诉我我做错了什么。

I'm doing some gpio stuff with my raspberry pi, and at the moment i'm making something so the gpio pins can be controlled via a web interface. 我正在用树莓派做一些gpio的工作,此刻我正在做一些事情,以便可以通过Web界面控制gpio引脚。 One of the ways I'm doing this is using bash CGI scripts to control the pins, and executing them from the browser. 我执行此操作的方法之一是使用bash CGI脚本控制引脚,然后从浏览器执行它们。

So far the only way I can get this to work involves the browser loading the page " .../cgi-bin/gpio1.cgi " etc, which contains the code: 到目前为止,我能使它起作用的唯一方法是浏览器加载页面“ .../cgi-bin/gpio1.cgi ”等,其中包含以下代码:

#!/bin/bash
echo "Content-type: text/html"
echo ""
...gpio stuff...

This works, but the browser navigates to the blank page created by this script. 这可行,但是浏览器导航到此脚本创建的空白页。

Is there a way of executing these scripts without leaving the webpage, and so the scripts aren't writing HTML but instead focusing on the actual gpio stuff? 有没有一种方法可以在不离开网页的情况下执行这些脚本,因此这些脚本不是在编写HTML,而是专注于实际的gpio内容?

Thanks 谢谢

Try this: 尝试这个:

#!/bin/bash
echo "Status: 204 No Content"
...gpio stuff...

HTTP responses must start with a status line; HTTP响应必须以状态行开头; webservers will normally add status "200 Ok" if the CGI doesn't specify one. 如果CGI未指定状态,则网络服务器通常会添加状态“ 200 Ok”。 That status must be accompanied by response body, which will form the new web page. 该状态必须带有响应正文,该响应正文将构成新的网页。

The status you want is 204 , which indicates that the request was satisfied but that there is no response and the browser should stay on the same page. 您想要的状态是204 ,它指示请求已满足,但没有响应,浏览器应停留在同一页面上。 Normally, this would be a response to a POST request, not a GET request, but it should work anyway. 通常,这将是对POST请求的响应,而不是对GET请求的响应,但是无论如何应该可以正常工作。 Since a 204 response does not require a response body (in fact, it doesn't permit one), it should not be necessary to output a blank line following the status line, but you might need one if the script takes a long time to run. 由于204响应不需要响应主体(实际上,它不允许一个响应主体),因此,没有必要在状态行之后输出空白行,但是如果脚本花费很长时间来响应,则可能需要一个空白行跑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM