将字符串从python服务器传递到嵌入式linux上的html页面

时间:2019-06-22 20:14:34

标签: python html web server

我正在使用运行有python服务器的DE1-SoC板。该开发板已嵌入Linux。启动服务器后,我从笔记本电脑的网络浏览器对其进行调用,并显示一个html网页。另外,python服务器正在从开发板的gsensor读取数据。 现在,我想做的是将服务器从gsensor读取的数据显示到网页上,并且每次刷新页面时,都会显示新数据。数据是一个简单的字符串,所以我想知道是否存在一种无需使用Flask框架即可实现此目标的方法。

这是我的python服务器代码:

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep
import shlex,subprocess

PORT_NUMBER = 8080

#This class will handles any incoming request from
#the browser 
class myHandler(BaseHTTPRequestHandler):

#Handler for the GET requests
def do_GET(self):
    if self.path=="/":
        self.path="/index.html"

    try:
        #Check the file extension required and
        #set the right mime

        sendReply = False
        if self.path.endswith(".html"):
            mimetype='text/html'
            sendReply = True
        if self.path.endswith(".jpg"):
            mimetype='image/jpg'
            sendReply = True
        if self.path.endswith(".gif"):
            mimetype='image/gif'
            sendReply = True
        if self.path.endswith(".js"):
            mimetype='application/javascript'
            sendReply = True
        if self.path.endswith(".css"):
            mimetype='text/css'
            sendReply = True

        if sendReply == True:
            cmd=["./gsensor", ""]
                        proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, 
 shell=True)
            line=proc.stdout.readline()


            #Open the static file requested and send it
            f = open(curdir + sep + self.path) 
            self.send_response(200)
            self.send_header('Content-type',mimetype)
            self.end_headers()
            self.wfile.write(f.read())
            f.close()
        return


    except IOError:
        self.send_error(404,'File Not Found: %s' % self.path)

try:
    #Create a web server and define the handler to manage the
    #incoming request
    server = HTTPServer(('', PORT_NUMBER), myHandler)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

变量行是我想要传递给html页面的内容。

0 个答案:

没有答案