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
總結
以上是生活随笔為你收集整理的python怎么启动服务器_如何通过cgi启动服务器端python程序?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python程序设计实验教程 翟萍 第五
- 下一篇: c#截取字符串后几位_基础库的字符串设计