Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Tornado frame to make simple confession Wall website in Python

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Tornado framework to make a simple confession wall website in Python", the editor shows you the operation process through an actual case, the method of operation is simple and fast, and is practical. I hope this article "how to use Tornado framework to make a simple confession wall website in Python" can help you solve the problem.

A brief introduction to Tornado Framework

In Python, there are three main WEB development frameworks, but today we mainly use the Tornado framework (although this framework is a lightweight framework that few people use)

Install the Tornado framework module code

Pip install tornado

Django

Flask

Tornado

Advantages of Tornado framework

Micro-frame, high performance

Asynchronous support

Shortcomings of Tornado framework

Less wheels, unlike the massive plug-in support of frameworks such as Django.

Lack of the best practice, the company does not use much, and there are few learning materials.

Tornado framework usage scenario

Build micro-services

The composition of the framework

First try Tornado to import the modules to be used this time: import timefrom tornado import web, ioloop, httpserver view class MainPageHandler (web.RequestHandler): def get (self, * args, * * kwargs): # corresponding to get request self.wrilt ('hello tornado') set routing app = web.Application ([(r "/", MainPageHandler),])

"/" means to visit the home page. For example, the local domain name is 127.0.0.1. If you visit this URL, you will visit the home page.

Set the front-end socket and call if _ _ name__ = = "_ _ main__": # front-end socket http_server = httpserver.HTTPServer (app) http_server.listen (8000) ioloop.IOLoop.current (). Start () to run the program, try the water first

When Tornado starts, it will not prompt you that you have started like Django or Flask.

Set up the home page and call the front-end file template # Home class MainPageHandler (web.RequestHandler): def get (self, * args, * * kwargs): self.render ('index.html', name=' confession Wall', messages=MESSAGES)

This time, the front-end file is called directly, and there is still a lot of material for this website.

Website: https://www.17sucai.com/

Set settings = {'template_path':' templates', # set template file path 'static_path':' statics' # static file path} express wall view class WishHandler (web.RequestHandler): def get (self, * args, * * kwargs): # corresponding to get request self.render ('wish.html',name=' confession wall') def post (self, * args * * kwargs): # get the data passed at the front end content = self.get_argument ('content', default=None) name = self.get_argument (' name', default=' Anonymous') if content: # add data MESSAGES.append ({'name': name,' content': content) 'id': len (MESSAGES) + 1, 'num': len (MESSAGES) + 1 'time': time.strftime ('% Y-%m-%d% Hself.redirect% MVR% S')}) # Jump self.redirect ('/') else: self.write ('content cannot be empty') message # message saved in global variable MESSAGES = [{'id': 1,' name': 'student' 'time':' 2022-02-10 21 num': 1600, 'content':' handsome, 'num': 1}]

Run the code to see the final effect

This is the end of the content about "how to use Tornado framework to make a simple confession wall website in Python". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report