gpt4 book ai didi

python - Tornado 中的 Django 用户

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:22 26 4
gpt4 key购买 nike

我一直在使用下面的代码来获取 Tornado 中的 django 用户:

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

我的问题如下:
Django 更改了日志记录配置,我再也看不到 tornado 的任何日志输出。如何重新初始化 Tornado 日志记录?还有其他方法可以将 Django 与 Tornado 集成吗?

非常感谢。

最佳答案

要重置 Django 日志记录,我使用以下命令:

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

当使用 django.conf 模块时,LazySettings 类在初始化 django 记录器的地方被初始化。我还必须使用初始化设置类重写代码:

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

关于python - Tornado 中的 Django 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16549429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com