如何知道CGI代码是否有效运行?

2024-03-28 10:58:51 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题是我调用一个函数来运行,我不确定它是否正在运行。我的网站托管在一个树莓pi上,使用lighttpd和一些位于/var/www/cgi-bin中的.cgi和.py。当我转到192.168.1.24/cgi-bin/test1.py这个位置时,它会运行,但当我单击触发事件的按钮时,我似乎什么都没有得到。脚本中还有系统命令,但它们似乎也不起作用。你知道吗

HTML代码:

<button style="height: 75px; width: 85px" onclick="bot_stop()">
<img style="height: 63px"src="http://images.clipartpanda.com/stop-sign-
clipart-119498958977780800stop_sign_right_font_mig_.svg.hi.png">
</button>

<script>
var xmlhttp;
function bot_stop()
{
xmlhttp.open('POST','CGI-Executables/test1.py',true);
xmlhttp.send(null);
}
</script>

CGI代码:

!#/usr/bin/python
import os

print  "Content-type: text/html\r\n\r\n"
print  "<html>"
print  "<head><title> code is executing</title></head>"
os.system("sudo service motion stop")
os.system("sudo service motion stop")
os.system("sudo motion")
os.system("sudo service motion start")
print  "<p> code ran </p>"
print  "</html>"

Tags: 代码pybinosvarhtmlservicesudo
1条回答
网友
1楼 · 发布于 2024-03-28 10:58:51

该cgi脚本返回整个页面的html,并且不需要post。它可能并不适用于您正在尝试的ajax调用。尝试在主html文件中添加一个简单的链接来启动

<a href='/cgi-bin/test1.py'>
  <img style="height: 63px"src="http://images.clipartpanda.com/stop-sign-clipart-119498958977780800stop_sign_right_font_mig_.svg.hi.png">
</a>

如果你想换一个职位,也许一个简单的表格也可以。你知道吗

<form method='post' action='/cgi-bin/test1.py'>
  <input type="submit" value="stop">
</form>

我认为ajax调用可能有错误的路径,以及其他一些问题。这可能有用。如果警报显示是200,它就会运行。你知道吗

function bot_stop() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4) {
      alert(this.status);
    }
  };
  xmlhttp.open('POST','/cgi-bin/test1.py',true);
  xmlhttp.send(null);
}

如果您只是通过ajax访问脚本,那么可以对其进行更改,使其只打印一个简单的“OK”而不是整个html页面。像这样的。。。你知道吗

#! /usr/bin/python
import os

# TODO: maybe check that this is a post and not a get before restarting
os.system("sudo service motion stop")
os.system("sudo service motion stop")
os.system("sudo motion")
os.system("sudo service motion start")

print "Content-type: text/plain"
print ""
print "OK"

-下一期-

如果服务器用户没有设置权限,您可能无法使用sudo运行这些命令。上面的脚本中并不真正需要Python,因此可以用以下内容代替它:

#! /bin/bash
sudo service motion stop
sudo service motion stop
sudo motion
sudo service motion start

echo Content-type: text/plain
echo
echo OK

要查看正在运行这些脚本的用户,请将浏览器指向脚本(称为/var/www/cgi-bin)/哇!嘘) (创建新脚本时,请记住chmod +x <scriptname>

#! /bin/bash
echo Content-type: text/plain
echo
whoami

然后将浏览器指向http://192.168.1.24/cgi-bin/whoami.sh

要查看尝试执行不同命令时发生的情况,请尝试以下操作

#! /bin/bash
echo Content-type: text/plain
echo
sudo echo 1
echo 2

如果sudo是问题所在,那么在命令行上,您可以sudo su <whatever user you get from the whoami script>并查看当您以他们的身份尝试脚本时会发生什么。你知道吗

如果sudo权限是问题所在-

一个解决方案是将四个restart命令放入它们自己的脚本中,并向lighttpd用户sudo授予运行该脚本的权限,而不使用如下密码:https://serverfault.com/questions/294661/how-to-grant-sudo-rights-only-to-specific-script-files。(或者找到另一种方法使这些命令在没有sudo的情况下工作——这可能是另一个问题的主题)


#!在上面的代码中是向后的。你知道吗

当您在命令行上运行cgi脚本来测试它们时,请尝试这样运行它们

./test1.py

不是

python test1.py

这将有助于确保解释器和权限设置正确。您可能需要像这样修复权限chmod +x test1.py

在某些情况下,服务器将使用此信息来运行脚本。在其他情况下,我希望您为扩展配置解释器,而这一部分都无关紧要。你知道吗

相关问题 更多 >