Python Simple Web server learning Note (ii)

Source: Internet
Author: User

The

tag:inf    its    return   text   something    with   imp   isp   hand   

Import Basehttpserver#-------------------------------------------------------------------------------class    RequestHandler (basehttpserver.basehttprequesthandler): "Respond to HTTP requests with info about the request."    # Template for page to send Back.          Page = "

The above code can output debugging Information.

Next we need to allow the server to provide static Files. The first thing we're going to do is exception handling

Import sys, os, basehttpserver#-------------------------------------------------------------------------------    Class Serverexception (Exception): ' for internal error reporting. ' Pass#-------------------------------------------------------------------------------class RequestHandler (    Basehttpserver.basehttprequesthandler): "If The requested path maps to a file, then file is Served.    If Anything goes wrong, An error page is Constructed.    "# How to display an Error. Error_page = "" "

Assuming that the program allows services using any file in the directory, it combines the path provided by the URL (starting with '/', basehttpserver automatically puts it into Self.path) to get the file path that the user Wants. If the file does not exist, or the path does not point to the file, the above method will report the error by fetching and throwing an Exception. If a file is matched, the Do_get method calls Handfile to read and return the content, and then calls Send_content to return the contents of the file to the Client. wow, Obviously we are opening files in binary mode (identified by ' RB ' and ' B ') and reading the entire file, so this method can't handle the big program OH

The detail is that the return code is 200 if we directly send_content (content) an error page, Hand_error. This error message is not good discriminant: hand_error processing is the page can not find the situation, should return 404.

The OS is the basic Python module, which is used to get the file path. Raise is used to throw an Exception. The specific use of hair is in conjunction with Try,except. Print some debugging information, you can clearly see the process of it

Python Simple Web server learning Note (ii)

Related Article

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.