如何在ApacheWeb服务器上从Javascript运行Python脚本?

如何在ApacheWeb服务器上从Javascript运行Python脚本?,javascript,python,html,ubuntu,apache2,Javascript,Python,Html,Ubuntu,Apache2,无法获取HTML文件来调用Ubuntu Apache 2上的JavaScript函数hello()。我见过类似的问题,解决方案是取消httpd.conf文件中的#JSDIR行的注释,但现在只有apache.conf,没有类似的行。我想调用python脚本accesstest.py,在单击HTML文件上的按钮后,从main.js函数将当前日期和时间写入文本文件。单独运行时,所有文件都能正常工作。我该如何解决这个问题 index.html代码: accesstest.py Python代码: 以下是

无法获取HTML文件来调用Ubuntu Apache 2上的JavaScript函数hello()。我见过类似的问题,解决方案是取消httpd.conf文件中的#JSDIR行的注释,但现在只有apache.conf,没有类似的行。我想调用python脚本accesstest.py,在单击HTML文件上的按钮后,从main.js函数将当前日期和时间写入文本文件。单独运行时,所有文件都能正常工作。我该如何解决这个问题

index.html代码:

accesstest.py Python代码:

以下是显示屏:

以下是我发现的一些不适合我的解决方案:


谢谢。

我认为您混淆了为浏览器编写的JS和为CLI编写的JS。您不能像这样在浏览器中执行Python
spawn
可能在nodejs中工作,但不能嵌入到HTML+Javascript中
<!DOCTYPE html>
<html lang="en">
<head>
    <title><demo</title>
</head>
<body>
    <h1></h1>
    <script src="main.js"></script>
    <button onclick="hello();">js function</button>
</body>
</html>
let myHeading = document.querySelector('h1');
myHeading.textContent = 'Hello world!';

function hello(){
    var spawn = require("child_process").spawn;
    var process = spawn('python,["accesstest.py"]);
    alert('hello');
}
import datetime
def execute():
    file = open('/path/to/file/pytest.txt', 'a+')
    file.write('test time: %s \n' % (datetime.datetime.now()))
    file.close()
execute()