Передача строки с сервера Python на HTML-страницу во встроенном Linux - PullRequest
0 голосов
/ 22 июня 2019

Я использую плату DE1-SoC, где у меня работает сервер Python.На плате есть встроенный 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-страницу.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...