views:

168

answers:

5

I understand that I should use mod_wsgi to run Python, and I have been trying to get that set up, but I'm confused about it:

This is a sample configuration I found for web.py:

LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias /appname /var/www/webpy-app/code.py/

Alias /appname/static /var/www/webpy-app/static/
AddType text/html .py

<Directory /var/www/webpy-app/>
    Order deny,allow
    Allow from all
</Directory>

So... I understand I have to configure my web server to point to the python application? Isn't there a way to use it like PHP, where, when you request a .py file, Python interprets it? How can I get my web server to the very basic state, where I can upload a file containing print "Hello World", request it, and have it say "Hello World"?

+3  A: 

I think you can use mod_cgi with apache and put in the URL of an accessible python file, with the first line of the script

#!/usr/bin/python

however this is a VERY inefficient way of accessing the python code because apache has to reload python every time the page is accessed. Ok for a one-off maintenance script only you call every now and then, but not suitable for active content accessed by users.

Edit: I didn't realise you were on Windows. Something similar should be possible. Try googling python apache cgi.

Edit: If you have apache working in cgi mode, you don't need to restart it every time. If the script is present and executable at the URL path given it will run. If it isn't then you'll get a 404 page not found error

Edit: I did a very quick Google search for 'python cgi' and found these slides from ten years ago by Python's creator. They document an obsolete version of the languange but the slides from 41 onwards may be useful to you. Like I say, people have moved away from scripting web applications using this method but if your requirements are simple it will still work. http://www.python.org/doc/essays/ppt/sd99east/index.htm

Edit: The best approach depends on what you are trying to do. Using a framework that handles the serving for you may be useful. I can recommend Web2py as very capable, secure framework that would allow you to write scripts and add them dynamically. It has a windows version that includes a simple web server, or you can configure apache as well. As everything is included you could be up and running within minutes if you read the introductory information. If you've not used web frameworks before and aren't familiar with lanugage such as "model view controller" then don't be put off. http://www.web2py.com/

sparklewhiskers
Okay, but how can I create python applications / scripts / whatever without having to reconfigure and restart my server for each new one I create? I mean, you _can_ buy hosting with python support, how do they do it? I'm sure you don't call them up and ask them to reconfigure everything for your new application code
Carson Myers
Well I understood the shebang line, and the limitations of CGI with python, but I'm just looking for a way to use Python in an efficient way that doesn't require me to reconfig and restart the server if I want to write a new script
Carson Myers
Do web hosts just use python in CGI mode then?
Carson Myers
It's not that the requirements are simple, but I am hoping to be able to distribute this -- either into multiple sites on one server or to other servers. It doesn't seem right that I should have to dig into the server config just to add the application to a new place, but CGI won't scale with the application
Carson Myers
Carson, most people who use Python web hosting aren't using a file-per-page paradigm like PHP and old ASP, but will have a long-running process like Django or Turbogears serving up resources.
Kylotan
@CarsonMyers I still don't understand what you're aiming for. If it's a maintenance script that you want to run on several servers then would running it periodically using crontab/Scheduled Tasks be suitable? If it's a web application, then use a framework like web2py even if you don't need the database functionality. If it's a distributed web application then multiple instances of a web framework with a load-balancing and data sharding may be suitable.
sparklewhiskers
okay, so the script it points to should just be the framework? I guess I'm just a little confused about how exactly python is used in the web-development world...
Carson Myers
@CarsonMyers Yes, that's where the WSGI comes in though. The framework (or master script) runs continuously. WSGI defines a way of apache (or another server) to interface to the continuously running master script. A framework like web2py includes a master script that then calls your functions (known as controllers) based on the URL. You can dynamically change the controllers. A framework like web2py includes a simple server, so you don't need to worry about configuring Apache for WSGI to start with while you're trying things out.
sparklewhiskers
+2  A: 

I don't think WSGI/mod_wsgi is compatible with what you want to do-- a WSGI application generally takes over URL parsing, which makes it incompatible with your "works like PHP" requirement.

If you don't want to use CGI, I think mod_python's Publisher is closer to what you want: http://www.modpython.org/live/current/doc-html/tut-pub.html

Ross M Karchner
+1  A: 

Wandering a bit afield from your original question, but trying to answer some of what's come up in reply to sparklewhiskers:

For an example of how WSGI works in a shared hosting environment, see these pages on Passenger's WSGI support at A Small Orange and Dreamhost, in which the necessary server configuration is done through a .htaccess file or a web panel.

I think Python hosting on shared servers is still relatively uncommon; I expect more Python applications are run on some sort of VPS or Google AppEngine.

Some Python webapps aren't launched from an apache or nginx module at all, they have their own persistent server, and if you want to have them on the same server as something else you configure your other server to proxy to your Python server. But that's another story entirely.

keturn
+2  A: 

Contrary to what others say, you can get mostly where you want. StackOverflow is however not a very good place to get an explanation of what to do. Instead, you would in this case be better off going over to the official mod_wsgi mailing list on Google Groups at:

http://groups.google.com/group/modwsgi

You can also get some ideas of the path required by reading:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines

For something truly akin to how PHP works in respect or URL mapping, you don't want to be using WSGIScriptAlias and instead should use the AddHandler method of designating files as being WSGI script files.

I would also advise reading:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode

That latter isn't the whole picture though in respecting of code reloading as there are changes in mod_wsgi 3.X which aren't documented in there which add even more options for automatic source code reloading.

Overall, although you could follow the PHP model of doing things, I would very much suggest a high level Python web framework. That in conjunction with daemon mode feature of mod_wsgi for triggering reloads by touching the WSGI script file is generally more than enough and more predictable.

Graham Dumpleton
Thanks for the answer. It's not that I want to keep the PHP way of doing things, I'm just considering python and wondering how difficult it will be to deploy my projects. For smaller ones, they tend to just stay on our own server. But I'm planning a larger one that might be distributed, and so far PHP is the most easily distributed one
Carson Myers
+2  A: 

Most similar to the PHP model is perhaps Python Server Pages, PSP.

mod_python has a PSP handler. The files you create look like this:

<html>
<body>
<%
for n in range(3):
  # This indent will persist
%>
<p>This paragraph will be 
repeated 3 times.</p>
<%
# This line will cause the block to end
%>
This line will only be shown once.
</body>
</html>

Spyce takes the PSP model a step further, as does Webware for Python.

This article is a good introduction to mod_python's PSP.

codeape