Django user in tornado

I used the code below to get the django user in a tornado:

from django.conf import settings import django.contrib.auth import django.utils.importlib import tornado from tornado.options import options import tornado.web import tornado.ioloop import sockjs.tornado class RouterConnection(sockjs.tornado.SockJSConnection): def get_current_user(self, info): engine = django.utils.importlib.import_module(django.conf.settings.SESSION_ENGINE) session_key = str(info.get_cookie(django.conf.settings.SESSION_COOKIE_NAME)).split('=')[1] class Dummy(object): pass django_request = Dummy() django_request.session = engine.SessionStore(session_key) user = django.contrib.auth.get_user(django_request) return user def on_open(self, info): user = self.get_current_user(info=info) if __name__ == "__main__": import logging Router = sockjs.tornado.SockJSRouter(RouterConnection) app = tornado.web.Application(Router.urls, debug=True, ) app.listen(settings.TORNADO_PORT) tornado.options.parse_command_line() tornado.ioloop.IOLoop.instance().start() 

My problem is this:
Django is changing the logging configuration, and I no longer see the log exit from the tornado. How can I reinitialize tornado registration? Is there any other way to integrate Django with Tornado?

Thank you very much in advance.

+4
source share
2 answers

In reset in django log, I use the following:

 logger = logging.getLogger('') for handler in logger.handlers: logger.removeHandler(handler) tornado.options.parse_command_line() 

When using the django.conf module, the LazySettings class is initialized, where django logger is initialized. I also had to rewrite the code using initialized settings classes:

 from django.conf import settings import django.contrib.auth import django.utils.importlib import tornado from tornado.options import options import tornado.web import tornado.ioloop import sockjs.tornado TORNADO_PORT = settings.TORNADO_PORT class RouterConnection(sockjs.tornado.SockJSConnection): def get_current_user(self, info): engine = django.utils.importlib.import_module(settings.SESSION_ENGINE) session_key = str(info.get_cookie(settings.SESSION_COOKIE_NAME)).split('=')[1] class Dummy(object): pass django_request = Dummy() django_request.session = engine.SessionStore(session_key) user = django.contrib.auth.get_user(django_request) return user def on_open(self, info): user = self.get_current_user(info=info) if __name__ == "__main__": import logging logger = logging.getLogger('') for handler in logger.handlers: logger.removeHandler(handler) tornado.options.parse_command_line() Router = sockjs.tornado.SockJSRouter(RouterConnection) app = tornado.web.Application(Router.urls, debug=settings.DEBUG) app.listen(settings.TORNADO_PORT) tornado.options.parse_command_line() tornado.ioloop.IOLoop.instance().start() 
+2
source

Use tornado.wsgi.WSGIContainer to migrate Django. Refer to this demo project for a working example.

-1
source

Source: https://habr.com/ru/post/1480771/


All Articles