A prototype that requests multiple files.
This is the most grass case, a hard-coded string in memory,
The real app will go to other ports or read the files on the hard drive.
#!/usr/bin/env python fromBasehttpserverImportHttpserver, BasehttprequesthandlerImportTimestarttime=time.time ()classRequestHandler (basehttprequesthandler):"""definintion of the request Hadnler.""" def_writeheaders (self, doc):"""Write The HTTP headers for the document. If There is no documnet, send a 404 error code;""" ifDoc isNone:self.send_response (404) Else: Self.send_response (200) #Always -Server up-HTML for-now.Self.send_header ('Content-type','text/html') self.end_headers ()def_getdoc (self, filename):"""Handle a request for a document""" GlobalStartTimeiffilename = ='/': return """""" eliffilename = ='/stats.html': return """<HTML><HEAD><TITLE>Status</TITLE></HEAD> <body > This server had veen running for%d seconds. </BODY></HTML>"""% int (time.time ()-starttime)Else: returnNonedefDo_head (self):"""Handle a request for headers only"""Doc=Self._getdoc (Self.path) self._writeheaders (DOC)defDo_get (self):"""Handle a request for headers and body"""Doc=Self._getdoc (Self.path) self._writeheaders (DOC)ifDoc isNone:self.wfile.write (""""""%Self.path)Else: Self.wfile.write (DOC) serveraddr= ("', 8765) Srvr=httpserver (serveraddr, RequestHandler) srvr.serve_forever ()
Learn more about the Socketserver module-B in Python