Use Python on MAMP

2020-06-18 09:48发布

问题:

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>')

Again, the point is just to see if it works. 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 ?

回答1:

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'



回答2:

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.



标签: python mamp wsgi