如何使用Django与Tornado Web服务器?
这很简单(特别是对于django 1.4)。
1 – 只需构建您的django项目(和应用程序),并确保它工作正常。
2-在根文件夹创建一个新的python文件(使用django-admin.py startproject的目录相同的目录)
3然后复制下面的代码,编辑os.environ [‘DJANGO_SETTINGS_MODULE’]行,并将其粘贴到新的.py文件中。
import os import tornado.httpserver import tornado.ioloop import tornado.wsgi import sys import django.core.handlers.wsgi #sys.path.append('/home/lawgon/') # path to your project ( if you have it in another dir). def main(): os.environ['DJANGO_SETTINGS_MODULE'] = 'myProject.settings' # path to your settings module application = django.core.handlers.wsgi.WSGIHandler() container = tornado.wsgi.WSGIContainer(application) http_server = tornado.httpserver.HTTPServer(container) http_server.listen(8888) tornado.ioloop.IOLoop.instance().start() if __name__ == "__main__": main()
Django 1.6应该是这样的:
import os import tornado.httpserver import tornado.ioloop import tornado.wsgi from django.core.wsgi import get_wsgi_application def main(): os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' # path to your settings module application = get_wsgi_application() container = tornado.wsgi.WSGIContainer(application) http_server = tornado.httpserver.HTTPServer(container) http_server.listen(8888) tornado.ioloop.IOLoop.instance().start() if __name__ == "__main__": main()