python使用python BaseHttpRequestHandler提供图像和数据

时间:2018-11-22 16:20:59

标签: python http multipartform-data basehttprequesthandler

嗨,我有以下代码。

class Handler(BaseHTTPRequestHandler):
    PAGE = """
    <!DOCTYPE html>
            <html>
                <head>
                    <title>
                    </title>
                    <link href='/favicon.ico' rel='shortcut icon'>
                    <style>
                        img {
                            width: 80%;
                            height: auto;
                        }
                       .left{
                           float:left;
                       }
                       .right{
                           float:left;
                       }
                    </style>
                </head>
                <body>
                   <div class="left">
                       <img src='/mjpeg'>
                   </div>
                   <div class="left">
                       <!-- SHOW DATA HERE -->
                   </div>

              """

       PAGE_AFTER = """
            </body>
        </html>
"""

visualization_queue = None
predictionObj = None

def __init__(self, visualization_queue, *args, **kwargs):
    self.visualization_queue = visualization_queue
    super().__init__(*args, **kwargs)


def do_GET(self):

    print('path hai ye')
    print(self.path)
    if self.path == '/mjpeg':
        self.send_response(200)
        self.send_header(
            'Content-type',
            'multipart/x-mixed-replace; boundary=--jpgboundary'
        )
        self.end_headers()

        while True:
            frame = self.get_info_from_queue() """<-- irrelevant"""

            if frame is None:
                print('no image from stream')
                time.sleep(1)
                continue

            try:
                ret, jpg = cv2.imencode('.jpg', frame)
                self.wfile.write(bytes("--jpgboundary\n", "utf8"))
                self.send_header('Content-Type', 'image/jpeg')
                self.send_header('Content-length', len(jpg))
                self.end_headers()
                self.wfile.write(jpg)

                # time.sleep(self.config.delay)
            except (BrokenPipeError, ConnectionResetError):
                print('connection closed')
                break
    elif self.path == "/favicon.ico":
        print('getting favicon')
        icon = io.open("Common/logo-alten.png", "rb").read()
        self.send_response(200)
        self.send_header('Content-type', 'mage/x-icon')
        self.send_header('Content-length', len(icon))
        self.end_headers()
        self.wfile.write(icon)
    elif self.path == "/":
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(bytes(self.PAGE + self.PAGE_AFTER, "utf8"))
    else:
        print('error', self.path)
        self.send_response(404)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(bytes('<html><head></head><body>', "utf8"))
        self.wfile.write(bytes('<h1>{0!s} not found</h1>'.format(self.path), "utf8"))
        self.wfile.write(bytes('</body></html>', "utf8"))

    return


class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""

我正在尝试在服务器的localhost:8080上显示视频。问题是,我能够成功发送视频帧,如下面的代码部分所示。

ret, jpg = cv2.imencode('.jpg', frame)
self.wfile.write(bytes("--jpgboundary\n", "utf8"))
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-length', len(jpg))
self.end_headers()
self.wfile.write(jpg)

我现在还想要以html的形式发送其他数据,例如

<ol>
  <li>Person: 0</li>
  <li>cars: 5</li>
</ol>

要显示在页面的侧面。有人能指出我正确的方向吗?有解决方案吗?

谢谢。

0 个答案:

没有答案