Must Pythonweb development use a framework? Is there any other method like PHP statements?

Source: Internet
Author: User
Tags nginx reverse proxy
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: First of all, remember that the earliest and most basic CGI program, the so-called web output, is simply the content output by the web server to the browser.
#! /Bin/bash

Cat < HTTP/1.0 200 OK

Hello
EOF
This can all be used as the simplest CGI program to run :)
CGI depends on the one-proc-per-request model, which is inefficient. later, we developed a module design embedded in the Apache server, such as mod_php.
PHP is the most common (traditional ?) In the form of deployment, mod_php is run as an additional component of the web server, so many things are completed by the default design and environment, such as communication with the HTTP server.
Python is more inclined to the general programming language. to do Web development, you can use Apache/Nginx as the frontend and HTTP server as your own.
If you use Apache/Nginx as the front-end, you need to understand the Python and HTTP server interface protocols, such as the popular FastCGI protocol. The most common interface for Python to use the FastCGI protocol is wsgi api. Web. py/cherrypy and many other frameworks are lightweight frameworks that provide WSGI protocol support and simple callback interface corresponding customer requests as the starting point of design.

In addition, Python can independently implement the HTTP protocol without relying on the web server (Apache/Nginx). For example:
Base/Simple/CGIHTTPServer
Based on SocketServer. TCPServer
Asyncore-based
Based on Twisted and so on

Methods are different. The answer to this question is wonderful. I don't want to answer it any more, but the question is for Python Web development. I will give a brief explanation of my opinion.
First, as Mr. Lang said, HTTP processing on all servers is a CGI-like principle. The system accepts standard input text streams that comply with the protocol and outputs the same protocol-compliant text streams from the standard output stream, that is, "request" and "response ".
Then speaking of the PHP that the landlord compares with Python, PHP is essentially composed of four parts. I listed them in the ascending order of the minimum subset:
  • Language and Language Runtime Environment (PHP Script), write *. the php script is then run on the terminal. it can often be equivalent to the sh script or python script. here, only the PHP language is used for running.
  • The standard library, that is, a series of built-in functions and built-in classes that can be used in PHP in addition to the minimum subset mentioned above.
  • During Web running, this part encapsulates text input streams in CGI mode to generate $ _ GET, $ _ POST, $ _ COOKIE, and a series of Web-related support, at the same time, when the script is executed, the standard output (echo printed Content) is also added with additional Content (Content-Type, status code, response header) output in the protocol.
  • Template engine: PHP is quite different from the built-in template engine. the template engine is a little different from the template used as a tool in other languages, in terms of syntax parsing, PHP supports native mixed writing with templates. The text in is not parsed as a syntax, but as an output directly. The PHP template also uses this implementation.
Let's look at Python. Python was not designed specifically for the Web like PHP at the beginning, so Python is designed like Java and Ruby, and is designed as a common tool language. A newly installed Python contains the following two parts:
  • Language and Language Runtime Environment (Python virtual machine)
  • Standard Library (the Python standard library is well-known and has a wide coverage)
Unlike PHP, Python Web is not implemented as part of the language. Because of the design goals of the general language, Python uses a standard library in the form of C extension, with native network programming support. That is to say, anyone can use the Python socket interface to write their own Web servers. Due to the equivalence of the Turing machine, it is also technically feasible to implement a php vm using Python.
But the vast majority of people do Web development with Python do not choose their own socket to implement a server, because Python official Web design standards-WSGI (PEP 333 http://www.python.org/dev/peps/pep-0333/ ). The standards defined by WSGI divide Web applications into WSGI applications and WSGI servers. The latter provides encapsulation of the standard input/output stream similar to the Web runtime of PHP. The former is similar to the PHP application written by the former, and Web development of the specific application is carried out in the encapsulated environment. We strongly recommend that you read PEP 333, which is helpful for understanding WSGI.
So far, WSGI has been widely supported in mainstream Python applications. The so-called Python Web framework is encapsulated in the Web development environment in line with WSGI Application specifications, if the landlord has the energy to read the source code of the Bottle framework, it will be very rewarding-there is only one file, however, the full implementation of WSGI is encapsulated into an environment of its own style (a Bottle instance is a WSGI Application that implements the _ call _ method ). A framework like Django has already exceeded the scope of the Web framework. It integrates ORM, form verification, and so on, a bit like RoR. It is equivalent to PHP's Web environment. it is a microkernel framework like Bottle.
Compared with PHP, Python lacks a template engine. Python itself does not provide mixed writing methods like PHP and template text. I suggest you forget this method, even in PHP, because it is a breeding ground for nightmares. The real template should be responsible for the view display logic in the application. in Python, jinja2 and mako provide such services. in PHP, remember not to write them in the template rather than in the template.

Back to the main problem line, do Python development use a Web framework? Not necessarily, but using a well-encapsulated WSGI Application framework allows you not to parse request headers and splice response headers by yourself. without a Web framework, you can only write scripts to remove PHP. The use of a framework, especially the WSGI Application framework, not only provides a good encapsulation for low-layer protocols, but also works with a wide range of WSGI servers under the WSGI convention. For example, if you deploy a Python Web application in Linux, you can use uwsgi and then use nginx Reverse proxy. However, if you develop a Python Web application in Windows, you can use wsgiref built in the standard library as the WSGI Server, my application does not need to be changed.

As for Python, it does not need to be embedded into the template like PHP. in history, there was a thing called PSP (not the PSP, but the Python Server Page, there are not many people to remember now. Isn't it more elegant to use a clear development method that separates domain logic, persistence, control flow, and view logic than to stack anything on a page?

In addition, if the landlord is Ada, you can ask the deputy webmaster to show you the code of the big site application system. It is a system written in ASP. after nearly five years of operation and maintenance, all logic is written in an ASP/PHP template. No one dared to maintain the situation even if the youth league committee made a request. because it was not moving well, a bomb exploded. You can't write a PHP interpreter in Python. if the "PHP-like statement" you mentioned above refers to embedding program code in HTML, some template engines can implement similar functions. for example, jinja2 can (partially) embed python code in the template, and then cooperate with other libraries, it allows you to develop web applications using python like writing php-but I personally think this kind of thing can be called a framework.

However, this development method that blends logic and views has lagged behind, so currently there may not be a python framework that allows you to develop completely like php. Generally, the python framework separates MVC clearly, and the template engine does not encourage writing too much logic in it.

Script languages such as Python, Perl, and Ruby were originally designed not for the Web, so they only need various libraries, frameworks, php, asp is designed to be embedded in html, but it does not mean that Web development is not a language born for Web development. as long as you are powerful enough, even c can be used to write web programs. In general, a web project that is too small or too large does not need to use a framework. The former is unnecessary, and the latter is limited.
The framework is used for small and medium-sized traditional web projects, with the aim of rapid development, detailed constraints, and convenient recruitment.
There are also many non-invasive, library-type web development components that can be used in unsuitable web projects. It is recommended that you go to the Flask framework's tutorial upstairs. I won't say much about it.
You can check
Http://www.modpython.org/
Or
Http://karrigell.sourceforge.net/en/index.html
Unlike php, python is not a pure language designed for web development.

So you have to understand that it is a scripting language first, and then there are various libraries based on this language to implement various functions. For example, the cgi Library is a very basic web development library. of course, it is too basic, so there are still many web frameworks for further abstract optimization.

The final answer is: if you want to find php, there is no python implementation that includes syntax and built-in libraries for web development. However, you may have a membership fee for web development, but you have learned how to do more things that php cannot do. I have read PEP 333, which has benefited a lot. it is strongly recommended that you check it out.

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.