Is it possible to stream output from a python subprocess to a webpage in real time?

前端 未结 1 1947
时光取名叫无心
时光取名叫无心 2021-02-06 13:40

Thanks in advance for any help. I am fairly new to python and even newer to html.

I have been trying the last few days to create a web page with buttons to perform tasks

1条回答
  •  逝去的感伤
    2021-02-06 13:51

    You need to use HTTP chunked transfer encoding to stream unbuffered command line output. CherryPy's wsgiserver module has built-in support for chunked transfer encoding. WSGI applications can be either functions that return list of strings, or generators that produces strings. If you use a generator as WSGI application, CherryPy will use chunked transfer automatically.

    Let's assume this is the program, of which the output will be streamed.

    # slowprint.py
    
    import sys
    import time
    
    for i in xrange(5):
        print i
        sys.stdout.flush()
        time.sleep(1)
    

    This is our web server.

    2014 Version (Older cherrpy Version)

    # webserver.py
    
    import subprocess
    from cherrypy import wsgiserver
    
    
    def application(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        proc = subprocess.Popen(['python', 'slowprint.py'], stdout=subprocess.PIPE)
    
        line = proc.stdout.readline()
        while line:
            yield line
            line = proc.stdout.readline()
    
    
    server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8000), application)
    server.start()
    

    2018 Version

    #!/usr/bin/env python2
    # webserver.py
    import subprocess
    import cherrypy
    
    class Root(object):
        def index(self):
            def content():
                proc = subprocess.Popen(['python', 'slowprint.py'], stdout=subprocess.PIPE)
                line = proc.stdout.readline()
                while line:
                    yield line
                    line = proc.stdout.readline()
            return content()
        index.exposed = True
        index._cp_config = {'response.stream': True}
    
    cherrypy.quickstart(Root())
    

    Start the server with python webapp.py, then in another terminal make a request with curl, and watch output being printed line by line

    curl 'http://localhost:8000'
    

    0 讨论(0)
提交回复
热议问题