Learn more about the Socketserver module-B in Python

Source: Internet
Author: User

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

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.