Flask as an example of how Python's framework is used

Source: Internet
Author: User
Tags button type http request in python

This article mainly introduces the use of Python framework, the text of the installation of the Flask framework as an example to explain the code based on the python2.x version, the need for friends can refer to the

Understanding the WSGI framework, we found that a Web App is a WSGI processing function that responds to each HTTP request.

But how to handle HTTP requests is not a problem, but the question is how to handle 100 different URLs.

Each URL can correspond to get and post requests, as well as put, delete, and so on, but we usually only consider the most common got and post requests.

One of the simplest ideas is to take the HTTP request information from the Environ variable, and then judge it individually:

?

1 2 3 4 5 6 7 8 def application (environ, start_response): Method = Environ[' Request_method '] path = environ[' Path_info '] if method== ' Get ' and path== '/': return Handle_home (environ, start_response) if method== ' POST ' and path= '/signin ': Return Handle_signi N (environ, start_response) ...

Just write down the code is certainly impossible to maintain.

The reason why the code can't be maintained is because the interface provided by WSGI is much higher than the HTTP interface, but it is lower than the processing logic of the Web app, and we need to be more abstract on the Wsgi interface, so that we can focus on using a function to process a URL. As for the mapping of URLs to functions, it is done by the web framework.

Because it's easy to develop a web framework in Python, Python has hundreds of open source web frameworks. Here we will not discuss the pros and cons of various web frameworks, directly choose a more popular web framework--flask to use.

Using flask to write web apps is simpler than WSGI interface (this is not nonsense, if it's more complicated than Wsgi, what's the framework for?), we first install flask with Easy_install or PIP:

?

1 $ Easy_install Flask

Then write a app.py that handles 3 URLs, respectively:

Get/: Home page, go back to homepage;

Get/signin: Login page, display login form;

Post/signin: Processes the Login form to display the login results.

Note Oh, the same url/signin have get and post two requests, mapped to two processing functions respectively.

Flask automatically associates URLs and functions through the Python adorner, so the code we write is like this:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 flask import flask from flask import request   app = Fl Ask (__name__)   @app. Route ('/", methods=[' get ', ' POST ']) def home (): Return '

The server running the Python app.py,flask is listening on port 5000:

?

1 2 3 $ python app.py * Running on Http://127.0.0.1:5000/Try

Open the browser, enter the home address http://localhost:5000/:

Home Display correct!

Then enter Http://localhost:5000/signin in the browser address bar to display the login form:

Enter preset username admin and password password, login successful:

Enter a different user name and password for the error, Login failed:

The actual web app should get the username and password, then go to the database query and then compare it to determine whether the user can log in successfully.

In addition to flask, the common Python web framework is:

Django: Universal Web framework;

web.py: a compact web framework;

Bottle: Similar to flask web framework;

Tornado:facebook Open source asynchronous web framework.

Of course, because it's not so hard to develop a Python web framework, we'll develop a web framework for ourselves later.

Summary

With the web framework, when we write Web applications, our focus is shifted from the WSGI processing functions to the url+ corresponding processing functions, which makes it easier to write web apps.

When writing URL-handling functions, it is also important to get user data from HTTP requests in addition to configuring URLs. The web framework provides its own APIs to implement these features. Flask Gets the contents of the form by request.form[' name '.

Note <> : More Wonderful tutorials please focus on Triple programming

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.