Python custom web framework, JINJA2

Source: Internet
Author: User

WSGI (Web server Gateway Interface) is a specification that defines the interface format between Web apps and Web servers written in Python, enabling decoupling between Web apps and Web servers.

The standalone WSGI server provided by the Python standard library is called Wsgiref.

Standard Web Framework

#!/usr/bin/env python#coding:utf-8from wsgiref.simple_server Import make_serverdef runserver (environ, start_response ):    start_response (' K OK ', [(' Content-type ', ' text/html ')])    return ' 

  

Customizing the web Framework

From wsgiref.simple_server import make_serverdef login ():    return ' login ' def index ():    data = open (' index.html ', ' R '). Read ()    return dataurl = (    ('/login/', login), ('/index/', index), Def runserver (environ, start_response):    start_response (' K OK ', [(' Content-type ', ' text/html ')])    Geturl = environ[' Path_info '] for    item in url:< C7/>if item[0] = = Geturl:            return  item[1] ()    else:        return ' 404 ' if __name__ = = ' __main__ ':    httpd = Make_server (", 8000, runserver)    print" Serving HTTP on port 8000 ... " Httpd.serve_forever ()

  

Classification of MVC and MTV code stacking

Mvc:model, View , Controller         
       &NBSP;&NBSP,     &NBSP,      model holds data operations, view holds HTML files, Controller responsible for logical processing

Mtv:model, Template, View
            &NBSP;&NBSP,     &NBSP,      model store data operations, storing HTML files, view responsible for logical processing

Django is MTV mode


Template Rendering Jinja2

From wsgiref.simple_server import make_serverimport timefrom jinja2 import templateimport sysreload (SYS) Sys.setdefaultencoding (' UTF8 ') #//resolve Unicodedecodeerrordef Login (): Return ' login ' def index (): data = open (' Index. HTML ', ' R '). Read () Current_time = Time.strftime ('%y-%m-%d%h:%m:%s ') template = template (data) #使用jinja2模板处理 Res Ult = Template.render (name= ' YANGMV ', age= '), Current_time=curren                             T_time, user_list=[' WJ ', ' SB ',], num=1, Return Result.encode (' utf-8 ') url = (('/login/', login), ('/index/', index), Def runserver (environ, start_respon        SE): start_response (' OK ', [(' Content-type ', ' text/html ')]) Geturl = environ[' Path_info '] for the item in URL: If item[0] = = Geturl:return Item[1] () Else:return ' 404 ' if __name__ = = ' __main__ ': httpd = m Ake_server (", 8000, runserver) print"Serving HTTP on port 8000 ... " Httpd.serve_forever ()

 

  

Python custom web framework, JINJA2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.