Using django model/authentication/authorization within Tornado
There is a project which is deployed within django. So its authentication system is built from Django itself.
But ususually we want to get good use of it. And we don't want to build another system to manage 'user' information.
So, we can use django within tornado. I mean use tornado more.
We can build an service application using tornado.
For example, there is a file for tornado which would look like this:
It's a hello world tornado app. save it as tornado_service.py
___________________
Hello, world
Here is a simple “Hello, world” example web app for Tornado:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world") application = tornado.web.Application([
(r"/", MainHandler),
]) if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
This example does not use any of Tornado’s asynchronous features; for that see this simple chat room.
___________________
Make django work within tornado
import tornado.ioloop
import tornado.web
import os
# the settings refers to a file settings.py (it's the django main project's settings.py )
# just add the route in the os.environ[]
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# now you can use the authenticate lib from django
# the settings could include many setting details such as 'database', 'path', and etc..
from django.contrib.auth import authenticate class MainHandler(tornado.web.RequestHandler):
def get(self):
username = self.get_argument("username")
password = self.get_argument("password")
user = authenticate(username= username, password = password)
if user:
self.write("hello " + user.username)
else:
self.write("Wrong username or password! :(" )
return application = tornado.web.Application([
(r"/", MainHandler),
]) if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
then run this tornado app:
$ python tornado_service.py
In your web browser,
go to http://127.0.0.1:8888/?username=jake&password=anderson
If the password matches, then you will get
hello jake
If doesn't, then
Wrong username or password! :(
Now you can make django work within torando.
Have fun! Happy hacking!
Using django model/authentication/authorization within Tornado的更多相关文章
- tornado with MySQL, torndb, django model, SQLAlchemy ==> JSON dumped
现在,我们用torndo做web开发框架,用他内部机制来处理HTTP请求.传说中的非阻塞式服务. 整来整去,可谓之一波三折.可是,无论怎么样,算是被我做成功了. 在tornado服务上,采用三种数据库 ...
- 【转】Django Model field reference学习总结
Django Model field reference学习总结(一) 本文档包含所有字段选项(field options)的内部细节和Django已经提供的field types. Field 选项 ...
- Django model字段类型清单
转载:<Django model字段类型清单> Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField ...
- Django:Model的Filter
转自:http://www.douban.com/note/301166150/ django model filter 条件过滤,及多表连接查询.反向查询,某字段的distinct 1.多表 ...
- Django model中 双向关联问题,求帮助
Django model中 双向关联问题,求帮助 - 开源中国社区 Django model中 双向关联问题,求帮助
- django 自定用户系统 以及 Django Model 定义语法
http://www.tuicool.com/articles/jMzIr2 django使用自己的用户系统 http://www.jianshu.com/p/c10be59aad7a Django ...
- Django Model field reference
===================== Model field reference ===================== .. module:: django.db.models.field ...
- Django model对象接口
Django model查询 # 直接获取表对应字段的值,列表嵌元组形式返回 Entry.objects.values_list('id', 'headline') #<QuerySet [(1 ...
- Django学习之四:Django Model模块
目录 Django Model 模型 MODEL需要在脑子里记住的基础概念 区分清楚,必须不能混淆的 class Meta 内嵌元数据定义类 简单model创建实例 数据源配置 接着通过models在 ...
随机推荐
- JS中5秒中跳转到其他页面
原文:JS中5秒中跳转到其他页面 <head> <meta http-equiv="Content-Type" content="text/html; ...
- 当有多于64合乎逻辑的cpu时刻,Windows 下一个Oracle db 实例启动(startup)什么时候会hang(待定)
Bug 9772171 - Database startup hangs on Windows when machine has more than 64 cores [ID 9772171.8] 该 ...
- 如果在VMware上免费下载驱动
1.在不登录,或者没有账号的情况下,进入VMare的驱动下载界面,可以发现有一个大大的Download按钮. 2.点这个按钮,会提示登录. 3.注册账号登录后,回到驱动下载界面,会发现Download ...
- LinQ—扩展方法
概述 本节主要解说扩展方法,涉及LinQ的详细知识不多. 扩展方法的描写叙述 .net framework为编程人员提供了非常多的类,非常多的方法,可是,不论.net framework在类中为我们提 ...
- js操作cookie方法
cookie cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密).定义于RFC2109和2965都已废弃,最 ...
- unix域套接字UDP网络编程
unix域套接字UDP网络编程,服务器如下面: #include <stdio.h> #include <stdlib.h> #include <string.h> ...
- rsync服务器
转自:http://www.mike.org.cn/blog/index.php?load=read&id=639###pp=0 [rsync实现网站的备份,文件的同步,不同系统的文件的同步, ...
- 自己动手实现Expression翻译器 – Part I
伴随.Net3.5到来的Expression,围绕着它产生了各种各样有趣的技术与应用,Linq to object.Linq to sql.Linq to sqllite.Linq to Anythi ...
- .net创建并安装windows服务案例
1. 创建windows服务[引用博文]: 1. 将这个服务程序切换到设计视图2. 右击设计视图选择“添加安装程序”3. 切换到刚被添加的ProjectInstaller的设计视图4. 设置servi ...
- Visual Studio 2013 Use HTTPS (SSL) On Web Application Projects
公司调试HTTPS接口会用到,原文:http://www.codeproject.com/Tips/766918/Visual-Studio-Use-HTTPS-SSL-On-Web-Applicat ...