diff --git a/2022/01/tornado_demo1/.gitignore b/2022/01/tornado_demo1/.gitignore index be2dd1b..d49b3c6 100644 --- a/2022/01/tornado_demo1/.gitignore +++ b/2022/01/tornado_demo1/.gitignore @@ -1,3 +1,3 @@ -.idea -*.iml -env +.idea +*.iml +env diff --git a/2022/01/tornado_demo1/Dockerfile b/2022/01/tornado_demo1/Dockerfile new file mode 100644 index 0000000..4b1bd2b --- /dev/null +++ b/2022/01/tornado_demo1/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.8-slim-buster +COPY ./src /app +COPY ./requirements.txt /app/requirements.txt +WORKDIR /app +RUN python3 -m pip install -r /app/requirements.txt +CMD [ "python3", "/app/server.py" ] diff --git a/2022/01/tornado_demo1/requirements.txt b/2022/01/tornado_demo1/requirements.txt index c3368df..85f64db 100644 --- a/2022/01/tornado_demo1/requirements.txt +++ b/2022/01/tornado_demo1/requirements.txt @@ -1 +1 @@ -tornado +tornado diff --git a/2022/01/tornado_demo1/client.py b/2022/01/tornado_demo1/src/client.py similarity index 96% rename from 2022/01/tornado_demo1/client.py rename to 2022/01/tornado_demo1/src/client.py index e72e58c..79116dc 100644 --- a/2022/01/tornado_demo1/client.py +++ b/2022/01/tornado_demo1/src/client.py @@ -1,45 +1,45 @@ -from tornado.ioloop import IOLoop, PeriodicCallback -from tornado import gen -from tornado.websocket import websocket_connect - - -class Client(object): - def __init__(self, url, timeout): - self.url = url - self.timeout = timeout - self.ioloop = IOLoop.instance() - self.ws = None - self.connect() - PeriodicCallback(self.keep_alive, 20000).start() - self.ioloop.start() - - @gen.coroutine - def connect(self): - print("trying to connect") - try: - self.ws = yield websocket_connect(self.url) - print("connected") - self.run() - except Exception: - print("connection error") - - @gen.coroutine - def run(self): - while True: - msg = yield self.ws.read_message() - if msg is None: - print("connection closed") - self.ws = None - break - else: - print("Got a message: " + msg) - - def keep_alive(self): - if self.ws is None: - self.connect() - else: - self.ws.write_message("keep alive") - - -if __name__ == "__main__": - client = Client("ws://localhost:8888/socket", 5) +from tornado.ioloop import IOLoop, PeriodicCallback +from tornado import gen +from tornado.websocket import websocket_connect + + +class Client(object): + def __init__(self, url, timeout): + self.url = url + self.timeout = timeout + self.ioloop = IOLoop.instance() + self.ws = None + self.connect() + PeriodicCallback(self.keep_alive, 20000).start() + self.ioloop.start() + + @gen.coroutine + def connect(self): + print("trying to connect") + try: + self.ws = yield websocket_connect(self.url) + print("connected") + self.run() + except Exception: + print("connection error") + + @gen.coroutine + def run(self): + while True: + msg = yield self.ws.read_message() + if msg is None: + print("connection closed") + self.ws = None + break + else: + print("Got a message: " + msg) + + def keep_alive(self): + if self.ws is None: + self.connect() + else: + self.ws.write_message("keep alive") + + +if __name__ == "__main__": + client = Client("ws://localhost:8888/socket", 5) diff --git a/2022/01/tornado_demo1/server.py b/2022/01/tornado_demo1/src/server.py similarity index 86% rename from 2022/01/tornado_demo1/server.py rename to 2022/01/tornado_demo1/src/server.py index 274bbf1..5ac1681 100644 --- a/2022/01/tornado_demo1/server.py +++ b/2022/01/tornado_demo1/src/server.py @@ -1,26 +1,28 @@ -import tornado.ioloop -import tornado.web -import tornado.websocket - -class MainHandler(tornado.web.RequestHandler): - def get(self): - self.write("Hello, world") - -class WebsocketHandler(tornado.websocket.WebSocketHandler): - def open(self): - print("Opened...") - - def on_message(self, message): - print("Got a message: " + message) - self.write_message(u"You wrote: {0}".format(message)) - - def on_close(self): - print("Closed...") - -if __name__ == "__main__": - application = tornado.web.Application([ - (r"/", MainHandler), - (r"/socket", WebsocketHandler), - ]) - application.listen(8888) - tornado.ioloop.IOLoop.current().start() +import tornado.ioloop +import tornado.web +import tornado.websocket + +class MainHandler(tornado.web.RequestHandler): + def get(self): + self.write("Hello, world") + +class WebsocketHandler(tornado.websocket.WebSocketHandler): + def open(self): + print("Opened...") + + def on_message(self, message): + print("Got a message: " + message) + self.write_message(u"You wrote: {0}".format(message)) + + def on_close(self): + print("Closed...") + +if __name__ == "__main__": + print("Start of the server...") + application = tornado.web.Application([ + (r"/", MainHandler), + (r"/socket", WebsocketHandler), + ]) + print("Starting to listen on port 8888") + application.listen(8888) + tornado.ioloop.IOLoop.current().start()