0

I'm interested in making a pretty basic Python webapp, literally just a form that takes in some input, calls a Python function on that input, receives results returned from that function, and then uses web development trickery to make them look pretty. I've seen numerous solutions using CherryPy, flask, bottle, web.py, all of which are great, but I haven't seen an example where any of those actually uses Apache. They all seem to be this all-in-one web server framework, which is totally overkill for me and not what I need. I already have Apache running here, so rather than start up a second web server and have to worry about keeping it running, I'd rather just have Apache serve my app along with everything else all day long.

We have the fairly standard http://myserver.com/~user/ setup with public_html dirs in /home/user, but for the life of me I can't figure out how to create a page that calls a Python function that is served up and has the Python code actually execute when browsing to http://myserver.com/~user/. Let's assume for the moment mod_python or even mod_wsgi is setup correctly...what do I need to do on my end? Do I need to call my script via a PHP exec(), which to me defeats the need for mod_python in the first place, or is there a more elegant solution (that actually uses mod_python)?

dmn
  • 965
  • 3
  • 13
  • 24

1 Answers1

0

Your concept of python scripts are totaly wrong. Don't treat them like PHP scripts where you can have

<html>
<?php
echo "<title>Hello World</title>";
?>
</html>

an mod_php will parse it and execute php code to form final html page.

It's better to say that you have python application where mod_python and mod_wsgi provides interface (gateway) for apache to communicate with your application.

I have been asked quite a lot of times if one can write python web application without framework. Of course you can but you have to deal with HTTP meet and bones like headers and respones codes. Look at http://lucumr.pocoo.org/2007/5/21/getting-started-with-wsgi/.

So I'd suggest using framework.

If you don't want to mess with running your application like deamon look at, for example, this page http://flask.pocoo.org/docs/deploying/mod_wsgi/. It shows how to run flask app with apache and mod_wsgi.

twil
  • 6,032
  • 1
  • 30
  • 28
  • Thanks for the links. I knew it wasn't exactly like PHP but from all of these examples with frameworks, I knew it was possible to call a Python function without wrapping it in PHP or even ajax. One question I have, though, from that second link...the configuration is global, right? So this line: `WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi` would need to be added for every application for every `~user`, right, something like `/home/user/public_html/yourapplication/yourapplication.wsgi`? – dmn Jul 17 '13 at 18:27
  • 1
    Just use `WSGIScriptAliasMatch` https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIScriptAliasMatch. This way you won't need to configure every single application. – twil Jul 17 '13 at 18:47
  • GOT IT. I'll make sure our sysadmin has that set up. Thank you so much. :) – dmn Jul 17 '13 at 18:59
  • 1
    Please don't use WSGIScriptAliasMatch unless you have a very good reason. You do not need it for what they are suggesting and there are some traps in using it. See details in http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines for better ways of doing it using AddHandler. – Graham Dumpleton Jul 18 '13 at 02:50
  • @GrahamDumpleton thanks for your note. I definitely overlooked `WSGIScriptAlias /wsgi/ /path/to/wsgi/scripts/` form and `SetHandler`/`AddHandler` directives for wsgi scripts. But what traps do you mean? As for me `SetHandler`/`AddHandler` is more burden for configuring `mod_rewrite`. I just don't like it. – twil Jul 18 '13 at 07:56
  • SCRIPT_NAME, which dictates where the WSGI application is mounted, can be screwed up by WSGIScriptAliasMatch if not used correctly. – Graham Dumpleton Jul 19 '13 at 03:02
  • @GrahamDumpleton as said in `mod_wsgi` docs most frameworks allow to override `SCRIPT_NAME`. And most of the time you need `SCRIPT_NAME = ''` – twil Jul 19 '13 at 07:26
  • An empty string for SCRIPT_NAME is generally only what you want if your WSGI application is mounted at the root of the site. – Graham Dumpleton Jul 20 '13 at 06:08