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)