python怎么启动服务器_如何通过cgi启动服务器端python程序?

问题在于:subprocess.call("python /home/pi/test.py")

它从名为“python/home/pi”的可执行文件中查看您的PATH/测试.py“-所有一个程序,名称中有空格。您需要传递一个参数列表,而不是命令行。如the docs解释:args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

所以,这样做:

^{pr2}$

它将查找名为“python”的可执行文件,并使用“/home/pi”运行它/测试.py”作为它的第一个参数(并且以“python”作为其第零个参数),这就是您想要的。在

它可能仍然不起作用-python必须位于您的特殊Apache用户的路径上,通过一个可访问的路径,并且是可执行的。通常通过sys.executable或{}或任何合适的方法比较好。在

同时,如果您不直接调用call并忽略结果,那么调试它会更容易(比如在并非完全不可能的情况下)。例如:ret = call(["python", "/home/pi/test.py"])

if ret:

print "Running test.py returned", ret

……或者try:

check_call(["python", "/home/pi/test.py"])

except Exception as e:

print "Tried to run test.py, got back", repr(e)

或者…你真的希望它的输出直接传递到CGI吗?如果没有,您可能需要捕获它的输出并对其执行某些操作,例如:try:

output = check_output(["python", "/home/pi/test.py"])

except Exception as e:

print "Tried to run test.py, got back", repr(e)

else:

print "test.py said", output

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值