Run Django on Tornado
2015-10-24 11:23
691 查看
Here’s the code to get Django running on Tornado. Explanation below.
I run from within my virtual environment using
python tornado_main.py
I don’t use the standard python manage.py runserver provided by django.
I built this because I wanted
A WebSocker server which also handled authentication.
To reuse all the nice django apps for authentication.
To keep it lightweight. There are numerous ways to embed websocket frameworks inside of django, but they all seemed too heavyweight for what I was trying to do here.
I adapted this from https://gist.github.com/654157, and I stuck this main file in the same top-level folder as my django app.
转载自https://thinkfaster.co/2015/01/run-django-on-tornado/ 感谢作者。嘻嘻
#!/usr/bin/python2.7 # # Author: Michael Williamson import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "vis.settings") from tornado.options import options, define, parse_command_line import django.conf import django.contrib.auth import django.core.handlers.wsgi import django.db import django.utils.importlib import tornado.httpserver import tornado.ioloop import tornado.web import tornado.websocket import tornado.wsgi import datetime import httplib import json import logging from tornado.options import options, define define('port', type=int, default=8080) class BaseWebSocketHandler(tornado.websocket.WebSocketHandler): def prepare(self): super(BaseWebSocketHandler, self).prepare() # Prepare ORM connections django.db.connection.queries = [] def finish(self, chunk = None): super(BaseWebSocketHandler, self).finish(chunk = chunk) # Clean up django ORM connections django.db.connection.close() if False: logging.info('%d sql queries' % len(django.db.connection.queries)) for query in django.db.connection.queries: logging.debug('%s [%s seconds]' % (query['sql'], query['time'])) # Clean up after python-memcached from django.core.cache import cache if hasattr(cache, 'close'): cache.close() def get_django_session(self): if not hasattr(self, '_session'): engine = django.utils.importlib.import_module( django.conf.settings.SESSION_ENGINE) session_key = self.get_cookie(django.conf.settings.SESSION_COOKIE_NAME) self._session = engine.SessionStore(session_key) return self._session def get_user_locale(self): # locale.get will use the first non-empty argument that matches a # supported language. return tornado.locale.get( self.get_argument('lang', None), self.get_django_session().get('django_language', None), self.get_cookie('django_language', None)) def get_current_user(self): # get_user needs a django request object, but only looks at the session class Dummy(object): pass django_request = Dummy() django_request.session = self.get_django_session() user = django.contrib.auth.get_user(django_request) if user.is_authenticated(): return user else: # try basic auth if not self.request.headers.has_key('Authorization'): return None kind, data = self.request.headers['Authorization'].split(' ') if kind != 'Basic': return None (username, _, password) = data.decode('base64').partition(':') user = django.contrib.auth.authenticate(username = username, password = password) if user is not None and user.is_authenticated(): return user return None def get_django_request(self): request = django.core.handlers.wsgi.WSGIRequest( tornado.wsgi.WSGIContainer.environ(self.request)) request.session = self.get_django_session() if self.current_user: request.user = self.current_user else: request.user = django.contrib.auth.models.AnonymousUser() return request class HelloHandler(tornado.web.RequestHandler): def get(self): self.write('Hello from tornado') class WebSocketHandler(BaseWebSocketHandler): def open(self): print 'Websocket opened' print 'Current user: ' + str(self.get_current_user()) def on_message(self, message): print 'Websocket got a message: ' + str(message) def on_close(self): print 'Websocket closed' class NoCacheStaticHandler(tornado.web.StaticFileHandler): """ Request static file handlers for development and debug only. It disables any caching for static file. """ def set_extra_headers(self, path): self.set_header('Cache-Control', 'no-cache, must-revalidate') self.set_header('Expires', '0') now = datetime.datetime.now() expiration = datetime.datetime(now.year-1, now.month, now.day) self.set_header('Last-Modified', expiration) def main(): wsgi_app = tornado.wsgi.WSGIContainer( django.core.handlers.wsgi.WSGIHandler()) tornado_app = tornado.web.Application( [ (r'/static/(.*)', NoCacheStaticHandler, {'path': 'fe/static'}), ('/hello-tornado', HelloHandler), ('/websocket', WebSocketHandler), ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)), ]) server = tornado.httpserver.HTTPServer(tornado_app) server.listen(options.port) tornado.ioloop.IOLoop.instance().start() if __name__ == '__main__': main()
I run from within my virtual environment using
python tornado_main.py
I don’t use the standard python manage.py runserver provided by django.
I built this because I wanted
A WebSocker server which also handled authentication.
To reuse all the nice django apps for authentication.
To keep it lightweight. There are numerous ways to embed websocket frameworks inside of django, but they all seemed too heavyweight for what I was trying to do here.
I adapted this from https://gist.github.com/654157, and I stuck this main file in the same top-level folder as my django app.
转载自https://thinkfaster.co/2015/01/run-django-on-tornado/ 感谢作者。嘻嘻
相关文章推荐
- django中使用jquery ajax post数据出现403错误的解决办法(两种方法)
- django使用ajax post数据出现403错误如何解决
- windows+apache+mod_python配置django运行环境
- python Django连接MySQL数据库做增删改查
- django自定义Field实现一个字段存储以逗号分隔的字符串
- Tornado Web服务器中处理空白字符的解决方案
- 在Python的Django框架中加载模版的方法
- 在Django中使用Sitemap的方法讲解
- 详解Django中的form库的使用
- Django中的CACHE_BACKEND参数和站点级Cache设置
- 剖析Django中模版标签的解析与参数传递
- Python中DJANGO简单测试实例
- Python的Django框架中的数据库配置指南
- 在Django的上下文中设置变量的方法
- 在Python的Django框架中编写编译函数
- 在Django的URLconf中进行函数导入的方法
- 在Python的Django框架中生成CSV文件的方法
- 在主机商的共享服务器上部署Django站点的方法
- 使用Python的Django框架中的压缩组件Django Compressor
- 对于Python的Django框架部署的一些建议