Django Framework, small Simple server build, get name, HTML read and URL read

Source: Internet
Author: User
Tags response code

This interface is the Wsgi:web Server Gateway Interface. Web Gateway Interface.

 fromWsgiref.simple_serverImportMake_server#Wsgiref is a server similar to Njax#call the Make_server under the server module#This is just a simulation server, it must be Njax or Apache later.#define functions, Parametersdefapplication (environ, start_response):#print (environ) #封装请求信息, key-value pairs display the requestor's informationStart_response ('OK', [('Content-type','text/html')])#response header text type, yes: (), (), Expression    return[b'']#b is the byte type here is the body to behttpd= Make_server ("', 8080, application)#with Makeserver, the first step is to run here, and then the application functionPrint('serving HTTP on port 8000 ...')#start listening for HTTP requests:Httpd.serve_forever ()

Run first, then browser input 127.0.0.1:8000 accessible, browser emulation client, PY emulation server

 fromWsgiref.simple_serverImportMake_server#Wsgiref is a server similar to Njax#call the Make_server under the server module#This is just a simulation server, it must be Njax or Apache later.#define functions, Parametersdefapplication (environ, start_response):Print("Path", environ["Path_info"]) path=environ["Path_info"]    ifpath=="/alex":        return[b'']    elifpath=="/tom":        return[b'']    Else:        return[b"404"] Start_response ('OK', [('Content-type','text/html')])#response header text type, yes: (), (), Expressionhttpd= Make_server ("', 8080, application)#with Makeserver, the first step is to run here, and then the application functionPrint('serving HTTP on port 8000 ...')#start listening for HTTP requests:Httpd.serve_forever ()

Input 127.0.0.1:8000/tom, Web page display Huanhuan Tom

 fromWsgiref.simple_serverImportMake_server#Wsgiref is a server similar to Njax#call the Make_server under the server module#This is just a simulation server, it must be Njax or Apache later.deffool (): F=open ("abc.html","RB") Data=F.read ()returnDatadeffool2 (): F= Open ("abc.html","RB") Data=F.read ()returnData#define functions, Parametersdefapplication (environ, start_response):Print("Path", environ["Path_info"]) Start_response ('OK', [('Content-type','text/html')]) path=environ["Path_info"]    ifpath=="/alex":        #return [b '         returnfool2 ()elifpath=="/tom":        #return [b '         returnfool ()Else:        return[b"404"] httpd= Make_server ("', 8080, application)Print('serving HTTP on port 8000 ...')#start listening for HTTP requests:Httpd.serve_forever ()

The entire application () function itself does not involve any part of parsing HTTP, that is, the underlying code does not need to be written by us, we are only responsible for considering how to respond to the request at a higher level. The application () function must be called by the WSGI server. There are many servers that conform to the WSGI specification, and we can pick one to use. Python has a built-in Wsgi server called the WSGIREF application () function, which is an HTTP handler that conforms to the WSGI standard, and it receives two parameters://environ: A Dict object that contains all the HTTP request information;//Start_response: A function that sends an HTTP response. In the application () function, call: Start_response ('OK', [('Content-type','text/html']) sends the header of the HTTP response, noting that the header can only be sent once, that is, the start_response () function can only be called once. The Start_response () function receives two parameters, one is an HTTP response code, one is a list of HTTP headers, and each header is represented by a tuple containing two str. Generally, the content should be-type hair to the browser. Many other commonly used HTTP headers should also be sent. Then, the return value of the function B''Sends the body as the HTTP response to the browser. With Wsgi, we are concerned with how to get the HTTP request information from the Environ Dict object, then construct the HTML, send the header through Start_response (), and return the body. 
Print(environ['Path_info']) path=environ['Path_info'] Start_response ('OK', [('Content-type','text/html')]) F1=open ("index1.html","RB") Data1=f1.read () F2=open ("index2.html","RB") Data2=F2.read ()ifpath=="/yuan":        return[Data1]elifpath=="/alex":        return[Data2]Else:        return["". Encode ('UTF8')]

Django Framework, small Simple server build, get name, HTML read and URL read

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.