How do I develop a python web framework?

Source: Internet
Author: User
What are the prerequisites, what to note, and which popular Python web frameworks are suitable for reference and learning?

Reply content:

Xie invited (@WHPython), for the first time to answer questions, you laughed at.

Then the first is embarrassed, because the small partners must also know that we are here to test, so I would like to write a little, on the one hand is to put some of the usual work, on the other hand, I also want to try to avoid involving too advanced content lead to install * failure (after all, too tender no work experience), Of course, there is another important reason is that the content is too complicated. OK, let's get started.

First you need to know the basic request processing process for a Web application. As an example of the simplest and most primitive Dynamic Web page, you click the link (GET), submit the form (POST), and then send a connection to the server. HTTP request(RFC2616 5.1, followed by HTTP 1.1 for example), there is at least a method (verb, is get post what, see RFC2616 9th), address (URL), HTTP version, may also bring cookies ( SessionThe general implementation mechanism), cache related information (RFC2616 13), user-agent strings and so on a bunch of information. For post requests We also have form content as Request Entity(RFC2616 7.2), which is the form you fill out.

So we have some data on the request, but in general it's still on the front-end server ( Reverse Proxy, such as Nginx, ignore Load Balancing, the reverse is Transparent, and does not take into account the situation of the bare WSGI container directly carrying the request), has not yet passed into the backend language (here is Python). We have a specific mechanism for each language to map the HTTP request information to the appropriate programming language category, called Web Server Interface(Web server interface), generic such as cgi/fcgi/scgi, specific to a language such as wsgi/psgi/rack/..., specific to an operating system such as ISAPI (the goods are still alive?) , some of which are no longer in use, are not mentioned. In short, 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 the Wsgi,
    • the processing of the request is mapped to a call to the application callable (Application (environ, start_response), and the inline code block is not supported? );
    • The request Information is mapped to the corresponding key value in the environ dictionary , such as the request method is mapped to environ[' Request_method ', and the requested "relative path" is mapped to the environ[' Path_info ' ] (over simplification; WSGI application mount point , the framework layer generally do not care about this, Mount WSGI applications are generally WSGI containers such as Gunicorn, UWSGI and other components of the work);
    • The action to send the response header is mapped to the call Start_response (status, Response_headers) (the optional third parameter exception information is not considered);
    • Returns the action that the response data is mapped to application return iterable .

The response is then returned from Python to the Web server, sent back to the browser, the browser responds to the content rendering, and a request is completed.


With this perceptual knowledge, what we are doing as the author of the Python Web development Framework is to provide the easiest possible means of development and the lowest possible framework overhead , based on the WSGI specification. That is, our code will work in the middle tier of Wsgi and business logic. architecture, the Web development framework is more or less consistent with the MVC design pattern (Django tube It's called MTV, in fact, almost). At the same time, because of the location of the framework in the middleware, coupled with its nature of encouraging modularity and code reuse, it naturally needs to provide abstractions for common HTTP operations. Here are some topics to start with:

  • The mapping of the request path to View/controller, the parsing of the request parameters (routing, also called routing).
      • A regular matching scheme, such as a simple regular expression parsing component built into Django, can parse regular expressions of common syntax, parse capturing groups into positional parameters, named capturing Groups parse into a keyword parameter.
      • There are also DSL scenarios, such as the Werkzeug routing component.
  • The processing of the request entity. form parsing , with the Web server for uploading file processing.
      • Normal urlencoded form, JSON form, text/plain data, multi-part form
      • multi-part attachment, attachment Operation API
      • Large file uploads (this is typically done by the front end server in a temporary file on the disk, for example Nginx).
  • session . HTTP is stateless (stateless), which is a very important feature. If there is no session, you make several requests in succession, but there is no means to prove that you are the same person/machine (you may be a proxy server).
      • Session Backend (memory data structure) that stores session data? File? Rdbms? Redis? Memcache? )
      • Security Mechanisms (HMAC or whatever, can refer to Beaker's secure cookie Implementation)
      • session Middleware in the request processing process (extracting a session from a cookie, extracting a session from a query string, extracting a session from a custom header, and so on)
  • View/controller interface . Unleash your creativity and use your engineering experience.
      • function-based or class-based views? Reference: Django, Bottle, web.py, Tornado and so on one vote framework approach
      • How the framework's optional mechanisms and services are exposed,
        • Decorator? (such as @login_required additional requirements)
        • Callback? (Can think of only tornado and twisted this asynchronous framework to do things, as well as the entire JS ecosystem is a callback (regardless of promise or something) of the idea)
      • How is the data structure of the incoming application (business logic) layer designed? (HttpRequest equivalents, names may not be clear)
      • How is the response data structure designed? (HttpResponse equivalents, Ibid.)
  • Database Operations Encapsulation . Web applications are basically data-centric, this component is necessary, but also to write reusable code must be a link, after all, the framework is abstract, database operations or bare SQL, then the production environment (such as MySQL Pgsql) is not dumbfounded.
      • relational database. One-Stop solution reference: Django ORM, SQLAlchemy, lightweight solution reference each database Python binding.
      • Non-relational database. Each database python binding (Pymongo, Riak, redis-py, etc.), this is no alternative, because all kinds of nosql libraries are adapted to a particular needs of the design, there is no need to replace each other, which means the re-selection of technology.
  • Not to be continued

What's Next:

    • The frame design idea of main response Ajax/api request
    • The idea of real-time web framework under Python
    • Philosophy of frame Design
    • Framework Performance Analysis method

I Caishuxueqian, major projects have not done, small projects have not how to take off, please read my point of view must pay attention to. Thank you very much for your last reading.



Changelog

    • 2014/12/28 18:25 for the first time, I was already in bed yesterday when I wanted to write. Client cannot edit rich text cry cry OH
      • I found rich text editing function, long-term write rest and markdown will not be edited with WYSIWYG
      • Add some examples to the previous content, fine-tuning some of the narrative, specifically, I am temporarily lazy to git library management
      • Plus the contents of the framework database operation
First, you're going to use Python.
Learn a simple web framework, such as flask, about what the framework should be and how it might be used.
Then read the bottle, a file, read it, almost.
The rest of the understanding of some requests and requests to deal with knowledge, do not understand the search is the beginning and finish, did a simple, today remembered to take out. Ladder1984/globefish GitHub First of all, you have a successful business site model, similar to the Linkedin,facebook such as large sites, of course, we can not write to such a large structure. But the premise of this site model has to be used by other people. Otherwise it is too much of a waste to write a frame for yourself. Bottle Single File
    1. Here to Add. Have friends say that there are so many frameworks out there that we need to develop one? Need, not to professional development framework, but in the process of development framework, can let me understand, understand the framework, development has great benefits, so I think web developers should have a knowledge of the framework (at least read the source).
    2. Writing a web framework is not a day or two, we can start by writing a simple beginning, constantly refactoring, iteration.
    3. Need to master some basics of knowledge, protocols, TCP, HTTP, WSGI. Network IO Model (blocking/non-blocking, Sync/async).
    4. Design, some good frameworks have some good design inside, so the people who develop on this framework feel good.
    5. Some of the existing frameworks, read some of the current pop-up framework can learn some new development techniques, the benefits are many. Tornado, flask, you can see the initial version of the framework when you first read it, and you can download it from http://github.com .
  • 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.