zoukankan      html  css  js  c++  java
  • 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!

  • 相关阅读:
    Linux内核驱动--硬件访问I/O【原创】
    Linux内核驱动--mmap设备方法【原创】
    Linux系统调用的运行过程【转】
    蓝牙Bluetooth技术手册规范下载【转】
    FarBox--另类有趣的网站服务【转】
    蓝牙HID协议笔记【转】
    linux 串口0x03,0x13的问题【转】
    CC254x/CC2540/CC2541库函数速查(转)
    BLE获取iphone mac地址的方法--【原创】
    用secureCRT操作ubuntu终端
  • 原文地址:https://www.cnblogs.com/spaceship9/p/3584947.html
Copyright © 2011-2022 走看看