Python implements a simple Web server
A recent requirement is to create a simple Web server that simply needs to be simpler, with the goal of requiring a background process to receive the request and then process and return the result, so I think of using Python.
First, you create a myapp.py file that defines a method that all requests go through, and you can process the passed URL and parameters in this method and return the result.
def myapp(environ, start_response): ‘200 OK‘ headers = [(‘Content-type‘‘text/html‘)] start_response(status, headers) if len(environ[‘PATH_INFO‘1: return"Hello World!" else: return"Hello {name}!".format(name=environ[‘PATH_INFO‘][1:])
It then creates a server.py file that launches an HTTP service and uses the app created above to receive requests and process
fromimport make_serverfromimport myapphttpd = make_server(‘‘8000, myapp)print"Serving HTTP on port 8000..."httpd.serve_forever()
Finally run "Python server.py" to start the service.
In the browser, enter the following URL to test the results
http://localhost:8000
Http://localhost:8000/kongxx
Python implements a simple Web server