在网页中单击按钮时运行python脚本

时间:2016-09-15 10:24:56

标签: python node.js

我在python中有代码从Pi读取不同的传感器数据,这些代码运行良好。但现在我想在网上做同样的事情。我正在运行一个显示网页的Node.js服务器,网页上有一些按钮,如温度按钮,湿度按钮等。

因此,点击按钮时,必须运行我已经拥有的相应传感器的python代码,然后传感器值必须返回并显示在网页上并打开新标签。

我看到很多使用ajax,烧瓶的问题,但没有人解决我的问题。

这是我的代码:

    var http = require('http');  
    var spawn = require('child_process').spawn;

    http.createServer(function(req, res) {  
    res.writeHead(200, {
    'Content-Type': 'text/html'
    });
    res.write('<!doctype html> <html> <title>Sensor Data</title>'+
    '<style type="text/css"> h1 {text-align:center;}
    .button1{position:relative;left:350px;top : 20px;
    background-color : green;outline: none;border : none;font-size : 20px;
    padding : 32px 16px;text-align : center;border-radius: 15px;
    box-shadow:0 9px #999;} .button2{position:relative;left : 550px;
    top : 20px; background-color : green;outline: none;
    border : none;font-size : 20px;padding : 32px 16px;
    text-align : center;border-radius: 15px;
    box-shadow: 0 9px #999;}.button3{position : relative;left : 350px;
    top : 100px; background-color : green;outline: none;border : none;
    font-size : 20px;padding : 32px 16px;text-align: center;
    border-radius: 15px;box-shadow: 0 9px #999;}
    .button4{position : relative;left : 550px;top : 100px; 
    background-color : green;outline: none;border : none;
    font-size : 20px;padding : 32px 16px;text-align: center;
    border-radius: 15px;box-shadow: 0 9px #999;}
    .button5{position : relative;left : 190px;top : 250px; 
    background-color : green;outline: none;border : none;
    font-size : 20px;padding : 32px 16px;text-align: center;
    border-radius: 15px;box-shadow: 0 9px #999;}
    .button1:hover, .button2:hover, .button3:hover, 
    .button4:hover,    .button5:hover 
    {background-color:  yellow}.button1:active, 
    .button2:active,   .button3:active, .button4:active,
    .button5:active {box-shadow: 0 5px #666;
    transform:   translateY(4px);
    background-color: yellow;}   
    </style>'+'<body> <h1>Sensor Buttons</h1></br>
    <button class="button1">Temperature Data</button> 
    <button class="button2"> Humidity Data </button></br>
    <button class="button3">Motion Detection </button>
    <button class="button4"> Sound Detection </button>
    <button class="button5"> Light Intensity </button></body>' + '</html>');
    res.end();
    }).listen(8888, 'localhost');
    console.log('Server running at http://localhost:8888');'

请指导我,因为我对Node.js很新。 感谢

0 个答案:

没有答案