def camera():
    if request.environ.get('wsgi.websocket'):
        ws = request.environ['wsgi.websocket']
while True:
    fp = StringIO()
image = cam.getImage().flipHorizontal().getPIL()
image.save(fp, 'JPEG')
ws.send(fp.getvalue().encode('base64'))
sleep(0.5)
if __name__ == '__main__':
    http_server = WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
http_server.serve_forever()
How to send a pygame image over websockets?