Python implements a simple Web server

Source: Internet
Author: User

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

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.