Simplehttpserver.simplehttprequesthandler inherited the Basehttpserver.basehttprequesthandler.
The source code mainly implements the Do_head () and Do_get () functions that need to be called during basehttpserver.basehttprequesthandler processing. This type of function is called after the Basehttprequesthandler accepts the request and judges the command in the request header.
def handle_one_request (self): ... mname = ' do_ ' + self.command If not hasattr (self, mname): self.send_error (501, "Unsupported Method (%r)"% Self.command) return method = GetAttr (self, Mname) method () ...
Therefore, it is essential to call this method () when we use Simplehttpserver for Web request processing, except, of course, other exception cases.
Simplehttpserver.simplehttprequesthandler The default processing is if the current directory in which the script is executed contains Index.html or index.htm, will be the HTML content of this file as the home page, if not exist, the interface displays the contents of the folder under the current directory, and set it inside the HTML page presentation.
deflist_directory (self, path):"""Helper to produce a directory listing (absent index.html). Return value is either a file object, or None (indicating an error). In either case, the headers is sent, making the interface the same as for Send_head (). """ Try: List=os.listdir (path)exceptOs.error:self.send_error (404,"No permission to list directory") returnNone list.sort (Key=LambdaA:a.lower ()) F=Stringio () Displaypath=Cgi.escape (Urllib.unquote (Self.path)) F.write ('<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 3.2 final//en" >') F.write (""%displaypath) F.write ("<body>\n"%displaypath) F.write ("") forNameinchList:fullname=os.path.join (path, name) displayname= Linkname =name#append/for directories or @ for symbolic links ifOs.path.isdir (FullName): displayname= name +"/"linkname= name +"/" ifOs.path.islink (FullName): displayname= name +"@" #note:a link to a directory displays with @ and links with/F.write ('<li><a href= "%s" >%s</a>\n'%(Urllib.quote (linkname), Cgi.escape (displayname))) F.write ("</ul>\n") Length=F.tell () f.seek (0) Self.send_response (200) Encoding=sys.getfilesystemencoding () Self.send_header ("Content-type","text/html; charset=%s"%encoding) Self.send_header ("Content-length", str (length)) self.end_headers ()returnFSimplehttprequesthandler in List_directory ()
In fact, the request of the socket is processed in socketserver.tcpserver, the processing of the Web request header is processed in the Basehttpserver.basehttprequesthandler, the type of the head, the version, etc. are processed. The response to the request is processed in subclass Simplehttpserver.simplehttprequesthandler.
So, how did Simplehttpserver.simplehttprequesthandler make the request for the above statement?
First, simple through the Send_head () function to pre-parse the request URL path, and then extract the path and the current directory path combination to get the absolute path address of the request, If a index.html or index.htm file exists under this path, the contents of this file will be opened and the content of the feedback header is written, the length of the content and the type of content is write, if there is no such file, the contents of the current directory will be obtained, and a file cache is written in an HTML-formatted content, stating the current directory Some content and set the hyperlink, so that the user clicks the server can correctly feedback the corresponding content.
We found that Send_head () actually sent the request header is set according to the request content, that is, in Send_head (), simple has the request data ready, so after send_head () only need to call Self.copyfile (f, self.wfile) writes the contents of a file object or cache file object to the request stream object.
As for the other functions, they are prepared for these.
* It is worth noting that when reading the local file feedback to the client, note that the file needs to be in the form of RB, that is, the binary way to read, so that the text stream to avoid the line, but also correct even if the length of the flow (the length is as part of the feedback head).
Python simplehttpserver Source Learning