1

I have a Python script that I'd like to be run from the browser, it seem mod_wsgi is the way to go but the method feels too heavy-weight and would require modifications to the script for the output. I guess I'd like a php approach ideally. The scripts doesn't take any input and will only be accessible on an internal network.

I'm running apache on Linux with mod_wsgi already set up, what are the options here?

2
  • What is the output; is it something you can use a template to do a generate HTML structure, and populate elements? Commented Dec 12, 2012 at 14:02
  • I should have been clearer it's just plain text, the script just print stuff to stdout I just was a quick and simple way to display the output through the browser. Commented Dec 12, 2012 at 14:12

2 Answers 2

3

I would go the micro-framework approach just in case your requirements change - and you never know, it may end up being an app rather just a basic dump... Perhaps the simplest (and old fashioned way!?) is using CGI:

  • Duplicate your script and include print 'Content-Type: text/plain\n' before any other output to sys.stdout
  • Put that script somewhere apache2 can access it (your cgi-bin for instance)
  • Make sure the script is executable
  • Make sure .py is added to the Apache CGI handler

But - I don't see anyway this is going to be a fantastic advantage (in the long run at least)

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

5 Comments

It really does need to be a short term dump so this is perfect.
@sudo_O I should note it's not tested though... but in theory should work (or at least enough for you to find working configs/examples)
Apart from call(['/bin/svn','update','/path/to/repo']) any ideas why this isn't executed?
@sudo_O This is where it gets tricky (debugging is a pain), it could be because apache is the owner of the interpreter and that user/group doesn't have access to the repo.... Simple work arounds like this are a bit harder to debug, but there is the cgitb module that may come in useful for handling tracebacks (but maybe start with the response and return code of the call for pointers)
You're right the apache user didn't have rights to the repo and I wasn't seeing any output because my scripts wasn't handling the output from the subprocess. Anyway all sorted now, thanks for all your help!
3

You could use any of python's micro frameworks to quickly run your script from a server. Most include their own lightweight servers.

From cherrypy home page documentation

import cherrypy
class HelloWorld(object):
    def index(self):
        # run your script here
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

ADditionally python provides the tools necessary to do what you want in its standard library using HttpServer

A basic server using BaseHttpServer:

import time
import BaseHTTPServer


HOST_NAME = 'example.net' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 80 # Maybe set this to 9000.


class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_HEAD(s):
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()
    def do_GET(s):
        """Respond to a GET request."""
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()
        s.wfile.write("<html><head><title>Title goes here.</title></head>")
        s.wfile.write("<body><p>This is a test.</p>")
        # If someone went to "http://something.somewhere.net/foo/bar/",
        # then s.path equals "/foo/bar/".
        s.wfile.write("<p>You accessed path: %s</p>" % s.path)
        s.wfile.write("</body></html>")

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

What's nice about the microframeworks is they abstract out writing headers and such (but should still provide you an interface to, if required)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.