python - 将变量从在树莓派上运行的 python 脚本发送到网络服务器

标签 python python-2.7 ubuntu raspberry-pi raspberry-pi3

我有一个在我的树莓派上运行的 python 脚本,它从连接到树莓派的 GPIO 输入的传感器输出数据。我需要一种简单的方法来从任何地方访问服务器上的实时数据。我看过 django 框架,但对于我需要做的事情来说似乎有点过分了。

最佳答案

你的意思是像this?

你可以像这样用一些 JS 创建一个静态网页:

<html>
<head>
    <title>My Pi</title>
    <script
      src="https://code.jquery.com/jquery-3.2.1.min.js"
      integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
      crossorigin="anonymous"></script>
</head>
<body>
    <div id="sensor1"></div>
    <a href="javascript:getSensor1()">Get data from sensor 1</a>
    <script type="text/javascript">
        function getSensor1() {
            $.ajax({
                type: "POST",
                url: "cgi-bin/cputemp.py",
                dataType: "html",
                success: function(msg) {
                    document.getElementById('sensor1').innerHTML = msg;
                },
            });
        }
    </script>
</body>

在相对于您的 html 页面的 cgi-bin/cputemp.py 目录中还有类似的内容。

#!/usr/bin/python
import cgi;
import cgitb;
import time
cgitb.enable()
import commands
import sys
import string
print "Content-type: text/html\n\n";
mytemp1 = commands.getoutput('/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -f1')
output = "Pi CPU Temp is: " + mytemp1
print output

关于python - 将变量从在树莓派上运行的 python 脚本发送到网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44185285/

相关文章:

python - 在 Python 2.7 中使用 unicodedata.normalize

python-re.findall 如何将内容分成组

bash - OCI 运行时创建失败 : container_linux. go:380

amazon-web-services - cfn-init 错误 : Unable to retrieve remote metadata : No credentials

node.js - 无法在 Ubuntu 13.04 上运行 lessc。连 "command not found"都没有

Python Tkinter : How do I avoid global variables in a changing class?

python - 如何在 PyQT 中绘制节点和边?

python - 当类中的函数返回一个值时,它返回到哪里?

python - Pandas - 处理分类数据中的 NaN

python - python请求中的Http重定向代码3XX