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