Show data on server




class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = threading.Condition()

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            # New frame, copy the existing buffer's content and notify all
            # clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)


class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(301)
            self.send_header('Location', '/index.html')
            self.end_headers()
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.send_header('Altitude', alt)
            self.send_header('Latitude', lat)
            self.send_header('Longitude', lng)
            self.send_header('Speed', spd)
            self.send_header('Heading', head)
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream.mjpg':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with output.condition:
                        output.condition.wait()
                        frame = output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e:
                logging.warning(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()


class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True


output = StreamingOutput()






<html>
    <head>
        <title>AERFOLT Live Stream</title>
    </head>
    <body>
        <center><h1>AERFOLT Live</h1></center>
        <center><img src="stream.mjpg" width="1280" height="720" /></center>
        <h2>Altiude: {alt}</h2>
    </body>
</html>

is this a coorect way to show variables?

Hi Paarth,

You have posted about 300 lines of code, what looks like your entire script. Did you expect us to study your code, hundreds of lines, to answer your question? We’re volunteers, working for free. You will likely get better answers from more people if you can provide more focused examples:

Please read this:

It might help for you to also read this discussion on Stackoverflow, where they struggle with exactly the same problem that we do here:

And then think more carefully about your question:

“is this a coorect way to show variables?”

Is which part of your code a correct way to show variables?

What information do you want to show? Who are you showing it to?

And the most important question of all:

When you run your code, does it show the information you want it to show? If the answer is Yes, then it is probably fine.