Python Web app: Wsgi Basics

Source: Internet
Author: User

At the bottom of django,flask,bottle and all other Python web frameworks is the Web Server Gateway Interface, referred to as WSGI. Wsgi for Python is like Servlets to Java-A common specification for Web servers that allow different Web servers and application frameworks to interact based on common APIs (basic tutorial Qkxue.net). However, for most things, the Python version implementation is fairly straightforward.

WSGI is defined in THE PEP 3333 protocol, and if you want to learn more after reading this article, the author suggests that readers read the introduction first.

This article will introduce you to the WSGI instructions from an application developer's perspective and show you how to develop the application directly through WSGI (if you can't wait).

Your first WSGI application

Here's the most basic Python web app:

?
def app (environ, START_FN):
Start_fn (' K OK ', [(' Content-type ', ' Text/plain ')])
return ["Hello qkxue.net world!\n"]
That's it! The entire file. Name it app.py and then run it on any WSGI compiled server, then you can get a Hello world and accompany a 200 response status code (mobile app development company Ty300.com). You can use Gunicorn to complete the installation and execution of Gunicorn App:app via Pip (Pip install gunicorn). This command tells Gunicorn to get the callable Wsgi from application variables in the application module.

Just now, very excited. Just three lines of code to run an app? That must be a record of some kind (not including PHP, because mod_php is working). I bet you want to go deeper now.

So what is the most important part of a WSGI application?

A WSGI application is Python callable, like a function, a class, or an instance of a class with a __call__ method
The callable application must accept two parameters: Environ, a python dictionary that contains the necessary data, START_FN, which is callable by itself.
The application must be able to invoke START_FN and two parameters: a status code (string), and a list of headers expressed in two tuples.
The application returns a convenient, iterative object that contains bytes in the return body--for example, each one contains only "hello,world! "A list of strings. (If the app is a class, it can be done in the __iter__ method)

Python Web app: Wsgi Basics

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.