从网站运行python脚本

时间:2016-01-28 10:13:32

标签: python angularjs

我只是需要一些指导,以便我在下面的问题上阅读更多内容:

我的网络驱动器上的文件夹中有一些python脚本,我每天运行,我想创建一个网站,我以列表的形式显示这些脚本,当我点击它们时,我希望它们能够运行。

有没有办法用AngularJs解决这个问题?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

好的,所以我有一个小节点服务器我为了你的目的改变了一点,我知道这不是一个理想的服务器,但这应该做的工作。对帖子请求的响应应该取决于你想要解雇python脚本的天气,或者你也想要它的输出。

从前端发送包含python脚本路径的数据的发布请求或其他内容,并自行更改发布案例。

使用角度循环来读取列表,该列表可以是目录中静态文件夹中提供的json

此服务器无论如何都不安全,可能会在短时间内被破解,但对于本地方案,它可以达到目的。

此外,如果您不想使用此功能,请记住您可以将列表从端点提供到页面并使用ng-repeat填充HTML,然后使用脚本相对路径发送帖子请求或后端的脚本名称和句柄路径,这将是外卖。抱歉,无法为前端发布任何内容。

var http = require('http');
var fs = require('fs');
var url = require('url');
var colors = require('colors');
var exec = require('child_process').exec;

var staticReg = /^\/static\/\w+\.\w+$/;;
var server = http.createServer(function(request, response){
    var url_parts = url.parse(request.url)
    if (request.method=="GET"){
        process.stdout.write("GET :: ".green + url_parts.path.yellow)
        if(staticReg.test(url_parts.path)){
            if (fs.existsSync("."+url_parts.path)){
                response.writeHead(200)
                response.write(fs.readFileSync("."+url_parts.path))
                response.end()
                process.stdout.write(" :: 200\n".green)
            }
            else{
                response.writeHead(404)
                response.end()
                process.stdout.write(" :: 404\n".red)
            }
            return
        }

        switch (url_parts.path){
            case "/":
                    process.stdout.write(" :: 200\n".green)
                    var home = fs.readFileSync("./index.html")
                    response.writeHead(200);
                    response.write(home);
                    response.end();
                    break;
            default :
                    process.stdout.write(" :: 404\n".red)
                    response.writeHead(404);
                    response.write("404 not found")
                    response.end();
                    break;
        }
    }

    if (request.method=="POST"){
        process.stdout.write("POST :: ".green + url_parts.path.yellow)
        var body = '';
        request.on('data',function(data){
            body+=data;
        });
        request.on('end', function(){
            switch(url_parts.path){
                case "/fire/":
                        body=JSON.parse(body)
                        process.stdout.write(" :: 200\n".green)
                        command='python '+body["script_path"]
                        exec(command,function callback(error, stdout, stderr){
                            // Do error checking here and send response.
                        })
                        // Or send response here.
                        response.writeHead(200);
                        response.write("Command fired.")
                        response.end();
                        break;
            }
        })
    }
})

server.listen(8002);
console.log("Listening to "+"localhost".blue+" on port "+ "8002".green);
server.on('error',function(err){
    console.log('Error happened: '+err.toString());
})