Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Python on MAMP

Tags:

python

mamp

wsgi

I'm slowly migrating from PHP to Python. In particular, as I work in webdev/webdesign I would like to display a basic HTML page using Python, using the following code:

#!/usr/bin/python
   
print('<html><head></head><body>This is a test</body></html>')

Sending the file online on my host as index.cgi, I've had no problem displaying the content of the file.

The problems start when I try to install the WSGI module on MAMP, or just to make Python work in general with it.

When it go to localhost/index.cgi the content of the file is displayed instead of its results.

I've followed half a dozen tutorials and none seems to work, I always encounter a problem at one point or another. It seems to come from the fact that Apache that comes with MAMP isn't built in a way that lets you add modules to it (such as wsgi).

This is also comes from the fact that I can't find any recent article on how to install Python on MAMP, they all either date from 2008 or 2009, with old versions of MAMP, Python and Macports.

Can somebody points me to the current procedure to make this work ?

EDIT : Ok after finding this article I gathered that MAMP by default don't process CGI scripts outside of the cgi-bin/ folder in MAMP/. So I modified the Apache conf file as explained, it now apparently reads the .cgi file but throws an error 500 with the content shown above. Is the code the culprit or is it MAMP's ?

like image 464
Maxime Fabre Avatar asked Oct 08 '22 08:10

Maxime Fabre


2 Answers

Got it to work, the problem were the missing CGI interpretation of MAMP outside of the cgi-bin/ folder (see original post) and the missing headers :

print 'Content-type: text/html\n\n'

like image 70
Maxime Fabre Avatar answered Oct 13 '22 15:10

Maxime Fabre


This is just standard CGI, nothing special here, no need for WSGI. You do need to install Python. You can install it wherever you like, as long as your script can find it. You see the line:

#! /usr/bin/python

that is where the script will try to find Python, so change it to your Python installation, or fix your Python installation to be there.

like image 1
Ali Afshar Avatar answered Oct 13 '22 15:10

Ali Afshar