Skip to content

Instantly share code, notes, and snippets.

@savex83
Forked from eliangcs/tornado_subprocess.py
Created September 22, 2018 09:24
Show Gist options
  • Select an option

  • Save savex83/44929035344f847a40a1bd10a75cb4b6 to your computer and use it in GitHub Desktop.

Select an option

Save savex83/44929035344f847a40a1bd10a75cb4b6 to your computer and use it in GitHub Desktop.

Revisions

  1. @eliangcs eliangcs revised this gist Jan 5, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tornado_subprocess.py
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,11 @@
    A minimal web server that runs shell commands, powered by Tornado and its
    Subprocess module. It does non-blocking IO and streams the response.
    To start the server::
    To start the server:
    $ python tornado_subprocess.py
    To send a shell command using httpie::
    To send a shell command using httpie:
    $ echo 'ls -l /' | http POST http://localhost:8899 --stream
    """
  2. @eliangcs eliangcs revised this gist Jan 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tornado_subprocess.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    """
    A minimal web server that runs shell commands, powered by Tornado and its
    Subprocess module.
    Subprocess module. It does non-blocking IO and streams the response.
    To start the server::
  3. @eliangcs eliangcs revised this gist Jan 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tornado_subprocess.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    """
    A minimal web server that runs shell commands, based on Tornado and its
    A minimal web server that runs shell commands, powered by Tornado and its
    Subprocess module.
    To start the server::
  4. @eliangcs eliangcs created this gist Jan 5, 2016.
    58 changes: 58 additions & 0 deletions tornado_subprocess.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    """
    A minimal web server that runs shell commands, based on Tornado and its
    Subprocess module.
    To start the server::
    $ python tornado_subprocess.py
    To send a shell command using httpie::
    $ echo 'ls -l /' | http POST http://localhost:8899 --stream
    """
    import shlex

    import tornado.ioloop
    import tornado.web

    from tornado import gen
    from tornado.iostream import StreamClosedError
    from tornado.process import Subprocess


    class MainHandler(tornado.web.RequestHandler):

    @gen.coroutine
    def post(self):
    cmd = shlex.split(self.request.body)
    yield self.run_subprocess(cmd)

    @gen.coroutine
    def run_subprocess(self, cmd):
    proc = Subprocess(cmd, stdout=Subprocess.STREAM,
    stderr=Subprocess.STREAM)
    yield self.redirect_stream(proc.stdout)
    yield self.redirect_stream(proc.stderr)

    @gen.coroutine
    def redirect_stream(self, stream):
    while True:
    try:
    data = yield stream.read_bytes(128, partial=True)
    except StreamClosedError:
    break
    else:
    self.write(data)
    self.flush()


    def make_app():
    return tornado.web.Application([
    (r'/', MainHandler)
    ])


    if __name__ == "__main__":
    app = make_app()
    app.listen(8899)
    tornado.ioloop.IOLoop.current().start()