Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python like PHP in Apache/Windows

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"?

like image 252
Carson Myers Avatar asked Jan 25 '10 09:01

Carson Myers


2 Answers

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.

like image 134
codeape Avatar answered Oct 18 '22 06:10

codeape


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://legacy.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/

like image 4
sparklewhiskers Avatar answered Oct 18 '22 07:10

sparklewhiskers