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