-
-
Save savex83/44929035344f847a40a1bd10a75cb4b6 to your computer and use it in GitHub Desktop.
Revisions
-
eliangcs revised this gist
Jan 5, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: $ python tornado_subprocess.py To send a shell command using httpie: $ echo 'ls -l /' | http POST http://localhost:8899 --stream """ -
eliangcs revised this gist
Jan 5, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. It does non-blocking IO and streams the response. To start the server:: -
eliangcs revised this gist
Jan 5, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ """ A minimal web server that runs shell commands, powered by Tornado and its Subprocess module. To start the server:: -
eliangcs created this gist
Jan 5, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()