How to Develop a Pythonweb framework?

Source: Internet
Author: User
What preparations do you need, what do you need to pay attention to, and what popular Pythonweb frameworks are suitable for reference and learning? What preparations do you need, what do you need to pay attention to, and what popular Python web frameworks are suitable for reference and learning? Reply: Thank you for inviting me (@ WHPython) to answer questions on zhihu for the first time.

First of all, I am sorry, because our friends must know that we are about to take the test now, so I just want to write something simple. On the one hand, I want to make up some of my homework, on the other hand, I should try my best to avoid installation failure due to too deep content (after all, too tender and no work experience). Of course, another important reason is that the content is too complex. Okay. Let's get started.

First, you need to know the basic request processing process of a Web application. Take the simplest and most primitive dynamic web page as an example. When you click the link (GET) and submit a form (POST), you send HTTP Request(RFC2616 section 5.1, followed by HTTP 1.1 as an example), there are at least methods (verb, GET or POST, For details, see RFC2616 section 9th), address (URL), HTTP version, may also contain cookies ( Session(RFC2616 section 13), User-Agent string, and so on. For POST requests, we still have the form content Request Entity(RFC2616 section 7.2), which contains the form content you entered.

So we have some request data, but now the data is still on the front-end server ( Reverse ProxyFor example, nginx, temporarily ignored Server Load balancer, Anyway, yes TransparentThe bare WSGI container does not directly carry requests ). We have specific mechanisms for each language to map HTTP request information to the corresponding programming language category, called Web Server Interface(Web server interface), General for example, CGI/FCGI/SCGI, specific to a language such as WSGI/PSGI/Rack /..., specific to an operating system such as ISAPI (is this product still alive ?), If you no longer use it, you will not mention it. In the Python world, this is WSGI (PEP 3333, Web Server Gateway Interface), which defines the Interface between the Python language and the Web Server. In WSGI,
  • Request Processing ProcessMappedApplication callableApplication (environ, start_response), does it support inline code blocks ?);
  • Request InformationMappedEnviron dictionaryFor example, the request method is mapped to environ ['request _ method'], the requested "relative PATH" is mapped to environ ['path _ info'] (over-simplified; WSGI application not mentioned at the momentMount pointThe framework layer generally does not need to care about this.WSGI containerWork of components such as gunicorn and uWSGI );
  • Send Response HeaderThe action of is mapped to call start_response (status, response_headers) (the optional third parameter exception information is not considered );
  • Return response dataIng to application returnsIterable.

Then the response is returned from Python to the Web server, and then sent back to the browser. The browser renders the response content and completes a request.


With this kind of perceptual knowledgePython Web development frameworkBased on the WSGI specification, the author must provide the most convenientDevelopment MethodsAnd as low as possibleFramework overheadThat is, our code will work on the middle layer between WSGI and business logic. In terms of architecture, Web development frameworks are more or less compliantMVC design pattern(Django calls it MTV. In fact, it is similar ). At the same time, because the framework is located in the middleware location, coupled with the nature that encourages modularity and code reuse, it is naturally required for common HTTP operations.Abstraction. Here we can start some topics:

  • Request Path To view/controller ing, Request Parameter Parsing (Routing(Also called routing ).
    • Regular ExpressionMatching Scheme. For example, Django has a built-in simple regular expression parsing component that can parse Regular Expressions in common syntaxes and parse capturing groups into location parameters, named capturing groups is parsed as a keyword parameter.
    • YesDSLSuch as the routing component of Werkzeug.
  • Request Entity processing.Form ParsingWith Web ServersUpload filesProcessing.
    • Normal urlencoded form, JSON form, text/plain data, multi-part form
    • Multi-part attachment and attachment operation API
    • Upload large files (this is usually stored by the front-end server in a temporary file on the disk, for example, nginx is implemented in this way ).
  • Session. HTTP is stateless, which is very important. If there is no session, you can make several consecutive requests without any means to prove that you are the same person/machine (You may be a proxy server ).
    • Stored session dataSession backend(Memory data structure? File? RDBMS? Redis? Memcache ?)
    • Security Mechanism(For HMAC or something, refer to beaker's secure cookie implementation)
    • In the request processing processSession Middleware(Extract sessions from cookies, extract sessions from query strings, and extract sessions from custom headers)
  • View/Controller Interface. Make full use of your creativity and use your engineering experience.
    • Function-based or Class-based views? Reference: Django, Bottle, web. py, Tornado, and other one-vote frameworks
    • The optional mechanisms of the framework and how services are exposed,
      • Decorator? (For example, @ login_required)
      • Callback? (We can only think of the asynchronous framework like Tornado and Twisted to do things, and the idea that the entire JS ecosystem is callback (not considering Promise or something)
    • How to Design the data structure of the incoming application (business logic) layer? (The HttpRequest equivalents may not be remembered)
    • How to Design the response data structure? (HttpResponse equivalent, same as above)
  • Database Operation Encapsulation. Web applications are basically data-centric. This component is very necessary and a must for writing reusable code. After all, the framework is abstracted, and database operations are still bare SQL, changing the production environment (for example, changing MySQL to pgsql) is not dumb.
    • Relational databases. For all-in-one solutions, see Django ORM and SQLAlchemy. For lightweight solutions, see bind Python to various databases.
    • Non-relational database. Python binding (pymongo, riak, redis-py, and so on) of various databases. There is no alternative solution, because all NoSQL databases are designed to meet certain special requirements, there is no need to replace each other, which means to re-select the technology.
  • To be continued

The following content:

  • Framework Design Ideas for responding to AJAX/API requests
  • Real-time Web framework in Python
  • Framework Design Philosophy
  • Framework Performance Analysis Method

I am not a beginner, I have never done a big project, and I have never taken off a small project. Please be careful when reading my point of view. Thank you very much for reading this article.



Changelog

  • The first time I added that I had to go to bed when I wanted to write it yesterday. The client could not edit rich text and cried.
    • I found that the rich text editing function is now available, and the reST and Markdown operations for a long time will not be edited in WYSIWYG mode.
    • I added some examples to the previous content and fine-tuned some descriptions. I am too lazy to manage the git library for the time being.
    • Added the framework database operation content.
First, you must use python
Learn a simple web framework, such as flask, and understand what the framework should be and how to use it.
Then read the bottle file. After reading it, it's almost the same.
The rest of the articles are about request and request processing. If you do not know how to search, you can start and end with a simple one. Today I think of it. ladder1984/globefish · GitHub. First of all, you have a successful business website model, such as linkedin and facebook. Of course, we cannot write such a large architecture. However, the website model must be used by others. Otherwise, it would be a waste of time to write a framework for yourself. Bottle single file
  1. Some may say that there are so many ready-made frameworks. Do we need to develop one? Yes, it is not a professional development framework, but a process of development framework that gives me a better understanding and understanding of the Framework, which is of great benefit to development, therefore, I think Web developers should have a certain understanding of the Framework (at least read the source code ).
  2. Writing a Web framework is not a matter of one or two days, so we can start by writing a simple one, and constantly rebuild and iterate.
  3. You need to master some basic knowledge, protocols, TCP, HTTP, WSGI. Network I/O models (blocking/non-blocking, Sync/Async ).
  4. Design, some good frameworks have some good designs in it, so that the developers on this framework feel very good.
  5. Ready-made frameworks, read some of the current popular frameworks can understand some of the new development technology, a lot of benefits. tornado, flask, at the beginning of the read can see the initial version of the framework, can be from the http://github.com.

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.