Basic Primer _python-modules and packages. Write a handler function for the WSGI server implemented by Wsgiref?

Source: Internet
Author: User
Tags response code

Brief introduction:

1. The browser sends an HTTP request

2. The server receives the request, generates the HTML document

3. The server sends the HTML document as the body of the HTTP response to the browser

4. The browser receives an HTTP response, removing the HTML document from the HTTP body and displaying

Description: As the Web application of the essence, the simplest is to save the HTML file, the use of HTTP Server software, receive user requests, read HTML from the file and return, the common static server is so, and to achieve the dynamic generation of HTML, the server software can not be generated for us, However, the server software basically supports the WSGI (Web Gateway Interface) protocol, which allows us to focus on using PY to write the web Business without caring for the underlying implementation of the request/process request/Send request


Interface Specification:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/88/FB/wKioL1gEOCTzvqh1AADw8d97wac667.png "title=" X.png " alt= "Wkiol1geoctzvqh1aadw8d97wac667.png"/>

def app (environ, start_response): Start_response (' OK ', [(' Content-type ', ' text/html ')] return ' 

Description: The interface definition is very simple, only need one accepts two parameters (environ to contain HTTP request information Dict,start_response as a callback function is mainly used to send the response header information, accept two parameters The first is the response code, the second is a header composed of a list of tuples, Can contain multiple header information, but can only be called once), return returns the body body

Note: The interface is defined but only makes sense if it is called, and there are many the server that regulates the WSGI specification , PY provides us with a compliant, but dedicated test WSGI server wsgiref that can be quickly and easily tested


Quick installation:

Pip Install--upgrade wsgiref


Full code:

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# oschina:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module from wsgiref.simple_server import make_server# Description: Import other modules def app (environ, start_response): start_respons E (' K OK ', [(' Content-type ', ' text/html ')]) return ' 

Note: Regardless of the complexity of the HTTP program, the portal is the WSGI processing function, all the input information of the HTTP request can be obtained through the environ environment variable, the HTTP response can be start_response () plus the function return value as body, But complex web applications, WSGI functions or too low-level, have sprung up with a number of great web frameworks to simplify web development.


This article is from "@Can up and No BB ..." Blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1862566

Basic Primer _python-modules and packages. Write a handler function for the WSGI server implemented by Wsgiref?

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.