Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/public/wp-includes/functions-formatting.php on line 76
I needed a little web server running to simply serve up some HTML files, and also trigger a couple function calls via GET requests.
Here’s the script I came up with. I think it would be useful if you just need a simple HTTP server for something and don’t want to install Apache or anything crazy like that.
Oh yeah, and another bonus with this little guy is that it’s supposed to be multi-threaded.
""" Serves files out of its current directory. Doesn't handle POST requests. """ import SocketServer import SimpleHTTPServer PORT = 8080 def move(): """ sample function to be called via a URL""" return 'hi' class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): #Sample values in self for URL: http://localhost:8080/jsxmlrpc-0.3/ #self.path '/jsxmlrpc-0.3/' #self.raw_requestline 'GET /jsxmlrpc-0.3/ HTTP/1.1rn' #self.client_address ('127.0.0.1', 3727) if self.path=='/move': #This URL will trigger our sample function and send what it returns back to the browser self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(move()) #call sample function here return else: #serve files, and directory listings by following self.path from #current working directory SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) httpd = SocketServer.ThreadingTCPServer(('localhost', PORT),CustomHandler) print "serving at port", PORT httpd.serve_forever()