import json
import tornado.ioloop
import tornado.web
from src.exec import Executor
class ShellHandler(tornado.web.RequestHandler):
async def long_running_task(self):
"""
get, post 로 호출 시 오래 걸리는 작업.
"""
executor = Executor()
stdout, stderr = await executor.run()
return stdout, stderr
async def post(self):
data = self.request.body.decode('utf-8')
try:
json_data = json.loads(data)
self.write(f'received => {json_data}')
except json.JSONDecodeError:
self.write(f'error => {data}')
async def get(self):
ip_address = self.request.remote_ip
stdout, stderr = await self.long_running_task()
self.write(f"{ip_address}<br>stdout={stdout}<br>stdout={stderr}")
def make_app():
return tornado.web.Application([
(r"/shell", ShellHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
print("Server is listening on port 8888")
tornado.ioloop.IOLoop.current().start()
댓글 없음:
댓글 쓰기