Python Simple Web Server Learning Note (iii)

Source: Internet
Author: User

Import sys, OS, basehttpserver#-------------------------------------------------------------------------------    Class Serverexception (Exception): ' For internal error reporting. '    pass#-------------------------------------------------------------------------------class Case_no_file (object):    ' File or directory does not exist. ' def test (self, handler): Return not Os.path.exists (handler.full_path) def act (self, Handler): Raise Serve Rexception ("' {0} ' not found". Format (Handler.path)) #-------------------------------------------------------------    ------------------class Case_existing_file (object): ' File exists. ' def test (self, handler): Return Os.path.isfile (handler.full_path) def act (self, handler): Handler.handle_ File (handler.full_path) #-------------------------------------------------------------------------------class    Case_directory_index_file (object): ' Serve index.html page for a directory. ' def index_path (self, handler): Return Os.path.join (Handler.full_path, ' index.html ') def test (self, handler): Return Os.path.isdir (han Dler.full_path) and Os.path.isfile (Self.index_path (handler)) Def act (self, handler): Handler.hand Le_file (Self.index_path (handler)) #----------------------------------------------------------------------------    ---class Case_directory_no_index_file (object): ' Serve listing for a directory without an index.html page. '        def index_path (self, handler): Return Os.path.join (Handler.full_path, ' index.html ') def test (self, handler): Return Os.path.isdir (Handler.full_path) and not Os.path.isfile (Self.index_path (handler)) def Act (SEL F, Handler): Handler.list_dir (handler.full_path) #---------------------------------------------------------------    ----------------class Case_always_fail (object): ' Base case if nothing else worked. '  def test (self, handler): Return True def act (self, handler):      Raise Serverexception ("Unknown object ' {0} '". Format (Handler.path)) #---------------------------------------------- ---------------------------------class RequestHandler (Basehttpserver.basehttprequesthandler): "If the requested P    Ath maps to a file, this file is served.    If Anything goes wrong, an error page is constructed. "' Cases = [Case_no_file (), Case_existing_file (), Case_directory_index_file (), CAs    E_directory_no_index_file (), Case_always_fail ()] # How to display an error. Error_page = "" "

The task is to present a list of directory contents when the path represents a directory rather than a file.
How do I handle a URL (Uniform Resource Locator)? This time we define a list of classes that can be put into the list in the future, eliminating the possibility of adding if in the source code, which enhances the reusability

And then there's still a situation in three: Existing_file,no_file,always_fail

Then we make the change: Add Class Case_directory_index_file, return "index.html" when Request "index.html", Case_directory_no_index return directory list when not present

When Python is programmed, it often deals with files and directories, which is not the OS module. The OS module contains common operating system features, regardless of the specific platform. The following list of commonly used commands

(Quoted from http://www.cnblogs.com/kaituorensheng/archive/2013/03/18/2965766.html)

1. Os.name ()--judging the platform that is now being applied, Windows returns ' NT '; Linux returns ' POSIX '

2. OS.GETCWD ()--Get the current work directory.

3. Os.listdir ()--Specify all files and directory names in all directories. Cases:

      

Listed in the form of a list, which does not distinguish between directories and files.

4. Os.remove ()--delete the specified file

5. Os.rmdir ()--delete the specified directory

6. Os.mkdir ()--Create a directory

  Note: This can only be done by building a layer, to make it available for recursion:os.makedirs ()

7. Os.path.isfile ()--Determines whether the specified object is a file. is return true, otherwise false

8. Os.path.isdir ()--Determines whether the specified object is a directory. is true, otherwise false. Example: 9. os.path.exists ()--verifies that the specified object exists. is true, otherwise false. Example:

os.path.split ()--Returns the directory and file name of the path. Cases:

This is just the two parts of the front and back apart. is to find the last one '/'. See Example:

OS.GETCWD ()--Get the current working directory (get present work dir)

Os.system ()-Executes the shell command. Cases:

Note : When you run the shell command here, if you want to invoke a variable before python, you can do so in the following way:

var=123os.  environ ['var// note here [] is "string" Os.system ('echo $var')      

Os.chdir ()--Change directory to the specified directory

os.path.getsize ()--Gets the size of the file, if it is a directory, returns 0

Os.path.abspath ()--Get the absolute path. Cases:

os.path.join (path, name)--Connection directory and file name. Cases:

17.os.path.basename (path)--Return file name

os.path.dirname (path)--Return file path

__NAME__ is if this code is running directly, __name__ is __main__.

Well ... In other words, this does not teach the server intelligence to give the directory structure ah Ah, it just changed the structure of the class and then added a special sentence into Ah ah. Sure you didn't tease me?

Code Source: lines or less

Python Simple Web Server Learning Note (iii)

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.