1

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?

3
  • I'd use Bottle. Commented Oct 17, 2012 at 21:46
  • 1
    I've heard good stories about Flask. Commented Oct 17, 2012 at 21:58
  • I've got flask working with a similar command but for this one I've got to stick to python and the standard library. Commented Oct 18, 2012 at 6:08

1 Answer 1

1

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().

Sign up to request clarification or add additional context in comments.

1 Comment

Hochl, thank you very much for that. Your little sample and explanation are excellent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.