gm3dmo
gm3dmo

Reputation: 345

Python program to serve the output of a command via HTTP

I wonder if anyone can help me web enhance a python script which runs a status command for various apps:

/app/app1/bin/status.sh
/app/app2/bin/status.sh

A check() function in the script creates a hash:

results = check_function()

results['app1']['status'] = 'running'
results['app2']['status'] = 'stopped'

My current solution is terminal only and a display_results() function prints the status of the environment to the terminal with appropriate escape sequences to colorize it:

display_results(results)

I'd like to "web enable" the command so that it can have report the results dictionary over the web.

I'm thinking I can just have it listen on a socket and write HTML through a string template or would it be better to have BaseHTTPServer do the listening but how to get it to serve the output of a command rather than the index.html file on a filesystem?

This is not a production application it's used as a tool for developers to do a mass check on the status of an app.

The terminal version works with python 2.4 (Centos 5.8) but there's nothing to stop me from using python 2.7

Any suggestions?

Upvotes: 1

Views: 165

Answers (1)

hochl
hochl

Reputation: 12930

Without writing the whole code, why not use something like this:

import BaseHTTPServer

class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        def do_GET(self):
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                self.wfile.write("<html><body>You accessed path: {}</body></html>".format(self.path))

BaseHTTPServer.HTTPServer(('', 80), MyHTTPRequestHandler).serve_forever()

No problem adding code to MyHTTPRequestHandler that calls some script or whatever using self.path as argument and adding the result to the formatted page. Where is the problem with the batteries for such a simple task? Additionally the subprocess module might help, especially subprocess.Popen(["echo", "test"]).communicate().

Upvotes: 1

Related Questions