running a python script from cgi-bin at other location

632 views Asked by At

As I understand, one way of running Python scripts on a web-server is to define cgi-bin directory, and then to put there my script.cgi. That can result in something like http://example.com/cgi-big/script.cgi.

But what if I need to call that script from a different location? I mean, let's say, the purpose of this script is to generates a web-page, and I want that page to be accessible at http://example.com/the/other/cute/location/. How should I act then?

Is the only way to use SSI via index.html, which would include something like that:

<!--#exec cmd="../../../script.cgi" -->

Or are there any other solutions?

2

There are 2 answers

0
pacifist On

There are many ways to run your script, at whatever location you like; mostly this is done through your webserver configuration. I'm going to assume you're using apache, but there will be a way to do all these things on other webservers too. The key directives will look something like:

Options ExecCGI SetHandler cgi-script

For what you're trying to do modpython also sounds to be a straightforward solution for what you're interested in. Have a read through this: http://modpython.org/python10/

Point is - you can configure an apachehandler to listen for requests on a specific directory, or specific filetype (or both) and have them get handled by modpython.

If you're more interested in the scripts being treated as traditional cgi that's still an option, fairly well documented here: http://httpd.apache.org/docs/2.0/howto/cgi.html Similarly to how you scan specify the modpython apachehandler to work based on a file type or location you can do the same for a cgi handler (or perl or php - you can see why this stuff being modular makes sense).

... for completeness, I'll also point out WSGI, which whilst it's still new and changing is probably closer to the direction of things in the future when it comes to server based scripts: http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface WSGI is going to seem harder to get your head around initially, I'd start with one of the earlier two options.

I'm quite confident I've not covered all the ways you can run python on a webserver... but I think I've steered you closed to what you wanted to know.

0
Daniel Roseman On

Well, this is one way of running Python scripts, but not a very good way. There are plenty of better ways of running Python scripts on a webserver, of which the best is a (micro) framework running via WSGI.

To answer your question though, you would use an Alias directive in your Apache configuration to map your URL to your script location. Note also that there's nothing magical about the cgi-bin directory: any directory can be used for executable scripts as long as you set the ExecCGI flag (and an AddHandler) in the Options directive for that directory.