Refactoring and adding dockerfile.

master
Tomasz Półgrabia 2022-01-16 12:13:55 +01:00
parent b76257688b
commit a2ae9772cd
5 changed files with 83 additions and 75 deletions

View File

@ -1,3 +1,3 @@
.idea
*.iml
env
.idea
*.iml
env

View File

@ -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" ]

View File

@ -1 +1 @@
tornado
tornado

View File

@ -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)

View File

@ -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()