Build Python HTTP Service

Source: Internet
Author: User

We often need to build HTTP services, but do not want to engage in the complex Apache,ISS Server, etc., then we can use Python to help us build the server. For example, I've talked about using Python to build XMLRPC services for server/client communication, but here's another question, if I need to show local files (compared to slices), but RPC doesn't have direct access to local files.

In this case, you only need to open a simple service, display the file under the specified folder, and then use the RPC service to tune the file address of the service.


Here is an example of setting up an HTTP service to display local files:

Import sysimport basehttpserverfrom simplehttpserver Import simplehttprequesthandlerhandlerclass = Simplehttprequesthandlerserverclass  = Basehttpserver.httpserverprotocol     = "http/1.0" port = 8024import Sockethostname = Socket.gethostbyname (Socket.gethostname ()) server_address = (hostname, port) Handlerclass.protocol_ Version = PROTOCOLHTTPD = ServerClass (server_address, handlerclass) sa = Httpd.socket.getsockname () print "Serving HTTP on ", Sa[0]," Port ", sa[1]," ... "Httpd.serve_forever ()

Access the local file by accessing the printed address of the program:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Build Python HTTP Service

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.