# -*- coding: utf-8 -*-
import os
import sys
import tornado.ioloop
import tornado.web
import tornado.wsgi
import tornado.httpserver
from django.core.handlers.wsgi import WSGIHandler # 设置 Django 设置模块,sets the default settings module to use
_HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.append(_HERE)
os.environ['DJANGO_SETTINGS_MODULE'] = "settings" def main(port):
# 利用 tornado 的http server处理消息请求
wsgi_app = tornado.wsgi.WSGIContainer(WSGIHandler())

# Application 处理请求消息
tornado_app = tornado.web.Application(
# FallbackHandler: A RequestHandler that wraps another HTTP server callback.
# http://www.tornadoweb.org/en/stable/web.html#tornado.web.FallbackHandler
[('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
]) # HTTPServer is a very basic connection handler. It parses the request headers and body
server = tornado.httpserver.HTTPServer(tornado_app)
server.listen(port)
tornado.ioloop.IOLoop.instance().start() if __name__ == '__main__':
try:
import setproctitle
setproctitle.setproctitle('www:' + sys.argv[1])
except ImportError:
pass
main(int(sys.argv[1]))

基本思想:利用 tornado 的 http server,处理消息请求。

官方文档有介绍:

  • WSGIContainer lets you run other WSGI applications and frameworks on the Tornado HTTP server. For example, with this class you can mix Django and Tornado handlers in a single server.

详细内容,见代码注释。

django - django 承接nginx请求的更多相关文章

  1. Django 部署到Nginx

    在网上搜了很多篇Django+uwsgi+Nginx的部署文章,忙了一下午头昏脑胀,最终完成了部署.部署文章流程讲解都很好,但在细节上或许缺乏一些注意力,导致我多篇文章来回切换在字里行间寻找蛛丝马迹. ...

  2. [django]windows下用Django,静态文件请求失败,出现UnicodeDecodeError

    问题:windows下用Django,静态文件请求失败,出现UnicodeDecodeError:'utf-8' codec can't decode byte 0xb0 in position 1: ...

  3. Django 部署 uwsgi + nginx + supervisor

    Django 部署 uwsgi + nginx + supervisor https://hacpai.com/article/1460607620615?p=1&m=0 zonghua • ...

  4. django是怎么处理请求的

    本文摘自 http://djangobook.py3k.cn/2.0/chapter03/ 我们在Django建立helloworld自定义页面中新建了站点,并能接受URL请求展示我们的页面,那Dja ...

  5. Django的跨域请求

    一 同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之 ...

  6. Django中Celery http请求异步处理(四)

    Django中Celery http请求异步处理 本章延续celery之前的系列 1.settings配置 2.编写task jib_update_task任务为更新salt jid数据 3.url设 ...

  7. django允许跨域请求配置

    django允许跨域请求配置 下载corsheader pip install django-cors-headers 修改setting.py中配置 在INSTALLED_APPS中增加corshe ...

  8. Django项目部署(django+guncorn+virtualenv+nginx)

    一.说明 为了django项目部署到生产环境上,能够稳定的运行,且能够同时指出http和https的访问,对django的部署进行了一些研究,决定采用django + gunicorn + virtu ...

  9. CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境, 记坑篇

    CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境 CentOS7 + Python3 + D ...

随机推荐

  1. 【POJ】【2987】Firing

    网络流/最大权闭合子图 胡伯涛论文里有讲…… sigh……细节处理太伤心了,先是count和ans输出弄反了,改过来顺序时又忘了必须先算出来ans!要是不执行一下Dinic的话count就无意义了…… ...

  2. Unsupervised Learning: Use Cases

    Unsupervised Learning: Use Cases Contents Visualization K-Means Clustering Transfer Learning K-Neare ...

  3. NGUI屏幕自适应解决方案

    NGUI研究院之自适应屏幕 http://www.xuanyusong.com/archives/2536 Unity3D研究院之使用Android的硬件缩放技术优化执行效率 http://www.x ...

  4. Caching Tutorial

    for Web Authors and Webmasters This is an informational document. Although technical in nature, it a ...

  5. ***php解析html类库simple_html_dom

    下载地址:https://github.com/samacs/simple_html_dom 一直以来使用php解析html文档树都是一个难题.Simple HTML DOM parser 帮我们很好 ...

  6. C# MessageBox 用法大全(转)

    C# MessageBox 用法大全 http://www.cnblogs.com/Tammie/archive/2011/08/05/2128623.html 我们在程序中经常会用到MessageB ...

  7. hdu 1270 小希的数表

    思路:一定有sum[1]=num[1]+num[2],sum[2]=num[1]+num[3]; 但是sum[3]不知道是由num[1]+num[4]还是num[2]+num[3],这就需要枚举一下了 ...

  8. 使用 C# 对文件进行压缩和解压

    C#中对文件压缩和可以使用两个类: GZipStream 类 此实例分为几个模块,分别为: 压缩函数: /// <summary> /// 压缩文件 /// </summary> ...

  9. Android Drawable 关于selector中state_pressed="true"的位置顺序

    界面中有一个按钮使用这样的样式: <?xml version="1.0" encoding="utf-8"?> <selector xmlns ...

  10. http://my.oschina.net/u/1185331/blog/502350

    http://my.oschina.net/u/1185331/blog/502350