Python Learning-writing a simple web framework (ii)

Source: Internet
Author: User
Tags python decorator

In the previous log has been discussed and implemented according to the application of the URL, after I read the official document BOTTLE.PY, according to the design of bottle rewrite again, mainly for reference to the design of Daniel's ideas.

A simple example of a bottle.py

Take a look at how bottle is used, and the code comes from http://www.bottlepy.org/docs/0.12/index.html:

 from Import route, run, Template@route ('/hello/<name>') def index (name):     return template ('<b>hello {{name}}</b>! ', name=name) run (host='localhost', port=8080)

Obviously, the bottle is routed using an adorner. Based on the design of bottle, I will write a simple framework.

Python decorator

An adorner, as the name implies, wraps a function. Dynamically adds functionality to a function without changing the function. No more details are being explored here.

The approximate framework

According to the definition of WSGI, a WSGI application must be callable. So here's an approximate framework for a WSGI application:

Class Wsgiapp (object):    def __init__ (self):        pass    def route (Self,path=none):        pass        def __call__ ( Self,environ,start_response):        return Self.wsgi (environ,start_response)    def wsgi (Self,environ,start_ Response):        Pass

Among them, the route method is to save Url->target. Here for convenience, save the Url->target in the dictionary:

    def Route (self,path=None):        def  Decorator (func):            = func             return func         return Decorator

Here return func comment out also can, ask God to explain AH!!

Then there are each method that implements Wsgiapp:

 classWsgiapp (object):def __init__(self): self.routes= {}    defRoute (self,path=None):defDecorator (func): Self.routes[path]=funcreturnfuncreturnDecoratordef __call__(self,environ,start_response):Print 'Pager'        returnSelf.wsgi (environ,start_response)defWsgi (self,environ,start_response): Path= environ['Path_info']        PrintPathifPathinchSelf.routes:status='OK'response_headers= [('Content-type','Text/plain')] Start_response (status,response_headers)PrintSelf.routes[path] ()returnSelf.routes[path] ()Else: Status='404 Not Found'response_headers= [('Content-type','Text/plain')] Start_response (status,response_headers)return '404 Not found!'app=Wsgiapp () @app. Route ('/')defindex ():return['This is index'] @app. Route ('/hello')defHello ():return['Hello'] fromWsgiref.simple_serverImportmake_serverhttpd= Make_server ("', 8000, App)Print 'start ....'Httpd.serve_forever ()

In this way, the prototype of a simple web framework is implemented, and if you add a regular expression to the path in the adorner, you can easily deal with the URL. The next log is to join the template engine jinja2.

Python Learning-writing a simple web framework (ii)

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.