Simple application of python tonado framework

In the previous article, we learned about the services of building tonado. For the convenience of our better use, we suggest that you follow the django framework to build.

First, set up static to place static files such as js, jquery, etc

Create templates file and put it into html

Create upfile file to place the uploaded file

Set up the views file. We write the views in this file

Create server.py

import tornado.web
import tornado.ioloop
import tornado.httpserver
import config #Here is the configuration file
from application import Application #Here is the established route Configurator

if __name__ == "__main__":
    app=Application()
    # application.listen(8888)
    httpServer=tornado.httpserver.HTTPServer(app)
    # httpServer.listen(8000)
    httpServer.bind(config.options["port"])
    httpServer.start()
    tornado.ioloop.IOLoop.instance().start()

Here we separate the service from the design code, and we can play happily

Create the configuration file in config.


import os
BASE_DIRS=os.path.dirname(__file__)
#parameter
options={
    "port":7001,
}

#Configure the static file path of tornoto
settings={
    'debug':True,
    'static_path':os.path.join(BASE_DIRS,"static"),
    'template_path':os.path.join(BASE_DIRS,'templates')
}

Next, build the application


import tornado.web
import views.index
import config
class Application(tornado.web.Application):
    def __init__(self):
        handlers=[
            (r'/',views.index.IndexHandler),
            (r'/json1',views.index.json1),
            (r'/redirect',views.index.RedirectHandler),
            (r'/Errorstack',views.index.Errorindex),
            tornado.web.url(r'/wenge',views.index.wenge,{'word3':'h1','word4':'h2'},name='wengegood'),
            (r'/zgr/(\w+)/(\w+)/(\w+)',views.index.zgr),
            (r'/postfile',views.index.PostFileHandler)
        ]#Here is the route, according to the actual situation
        super(Application,self).__init__(handlers,**config.settings)#Inherit toserver.py Of Aplication
The next step is views Build a simple view to play


import tornado.web
from tornado.web import RequestHandler
#name finds the route
class IndexHandler(RequestHandler):
    def get(self,*args,**kwargs):
        url=self.reverse_url("wengegood")
        self.write("<a href='%s'>Go to another interface</a>"%url)
        # self.write("gggggg")
        #
        # self.write("sunck is a good tornado")

#json format return interface
class json1(RequestHandler):
    def get(self,*args,**kwargs):
        per={
            'name':'wenwen',
            'age':20,
            'sex':'male'
        }
        self.write(per)
class RedirectHandler(RequestHandler):
    def get(self,*args,**kwargs):
        self.redirect("/")#redirect
#Custom error
class Errorindex(RequestHandler):
    def write_error(self, status_code, **kwargs):
        if status_code==500:
            code=500
            self.write('Server error')
        if status_code==404:
            code=404
            self.write('Resource does not exist')
        self.set_status(code)

    def get(self,*args,**kwargs):
        flag=self.get_query_argument("flag")
        if flag=='0':
            self.send_error(404)
        self.write('hello tornoto')
#Background incoming parameters
class wenge(RequestHandler):
    def initialize(self,word3,word4):
        self.word3=word3
        self.word4=word4
    def get(self, *args, **kwargs):
        print(self.word3,self.word4)
        self.write('wenge is a nice men')

#url incoming parameters
class zgr(RequestHandler):
    def get(self,a,b,c,*args, **kwargs):
        print(a,b,c)
        self.write('zgr is a nice man')
#post receive parameters
class PostFileHandler(RequestHandler):
    def get(self, *args, **kwargs):
        a={'a':'sdfa'}
        self.render('1.html',a)
    def post(self,*args, **kwargs):
        name=self.get_body_argument('username')
        passwd=self.get_body_argument('passwd')
        hobbylist=self.get_body_arguments('hobby')
        print(name,passwd,hobbylist)
        self.write('Full name%s,Password%s,hobby%s'%(name,passwd,hobbylist))



Tags: Python Django JQuery JSON

Posted on Tue, 03 Dec 2019 23:53:26 -0800 by Tainted Kitten