Reference: Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001386832689740b04430a98f614b6da89da2157ea3efe2000
Code:
hello.py
1 #!/usr/bin/python2 #Coding:utf-83 4 #hello.py5 defapplication (environ, start_response):6Start_response ('OK', [('Content-type','text/html')])7 return ''% (environ['Path_info'][1:]or 'Web')
server.py
1 #!/usr/bin/python2 #Coding:utf-83 4 #server.py5 fromWsgiref.simple_serverImportMake_server6 fromHelloImportApplication7 8 #Create server, IP is empty, port was 8000, handle function is application9httpd = Make_server ("', 8000, application)Ten Print "serving HTTP on port 8000 ..." One #Start Listen HTTP request AHttpd.serve_forever ()
The module wsgiref is used. It implements the Wsgi interface, we only need to set a WSGI processing function to handle the resulting request.
Using Python to implement these seemingly complex instance programs is very simple, thanks to Python's powerful libraries.
Python Simple Web Server example