6

I'm using mamp server for testing out all my web pages. I'm new to python. I'm able to run a script in python interpreter that will normally print a hello world.

print "Hello World!"   

So i used the same line in a file with name test.py . So how should I run this on web.

As am new to python, i tried some normal things, placing test.py in /htdocs/cgi-bin/ and trying to open it. But it says forbidden page.

Anyone please help me making this work. Thanks

RaviTeja
  • 828
  • 3
  • 12
  • 20

2 Answers2

5

To do this with CGI, I recommend reading the Python CGI docs. At a minimum, you need to output the content type and html tags:

print "Content-Type: text/html"
print
print "<html>"
print "<head>"
print "<title>Example of Python CGI script</title>"
print "</head>"
print "<body>"
print "Hello World!"
print "</body>"
print "</html>"

Also, make sure the web server software has permission to execute the script. You should be able to use chown to set the ownership and chmod to set the permissions.

GreenMatt
  • 16,928
  • 6
  • 49
  • 75
  • hi @matt i'm very new to web applications can u please be more elobarative with how to run the commands. I'm using a MAMP server and my local host redirects to htdocs/. Where should I place the python file and how to run the chmod commands? – RaviTeja Feb 09 '11 at 15:30
  • @Ravi Teja: I've never worked on MAMP; hopefully my Linux & WAMP experience suffices. Also, some of this is configuration dependent, so you may need to adjust the following for your system. Putting the script in /htdocs/cgi-bin should work, assuming that's what your server's configuration is set for. Run chown & chmod from the command line; it's easiest to be in the same directory as the file. I recommend doing a `chown apache:apache hello.py` to set the file's ownership to the webserver (assuming the server runs under user apache). Then a `chmod u+x` to allow the program to be run. – GreenMatt Feb 09 '11 at 15:49
  • Thanks for your quick reply, SO when i try running the chown command from the terminal going into the cgi-bin directory i got an error chown: apache: Invalid argument – RaviTeja Feb 09 '11 at 16:03
  • Unfortunately, this is difficult to diagnose via a forum - at least for me. A first guess is that the user which runs your server isn't called apache. Figure out what user the server does run under and use that username. Alternatively, you could just `chmod o+x hello.py` to make the file runnable by anyone. That will allow you to test it. I've seen operational systems set to run that way. However, some people have security concerns with such a set up - if this is a development system and you're not allowing the outside world to see the site, it's probably not an issue. – GreenMatt Feb 09 '11 at 17:51
  • @GreenMatt Hi Matt, How can i get the users using apache and for supppose if the user is root what command should i try chown root:apache hello.py or chown apache:root hello.py or chown root:root hello.py – RaviTeja Feb 10 '11 at 04:55
  • @Ravi Teja: It seems you're really at a beginner level on most things here. I recommend you learn a lot more about your operating system. Presumably you're using a version of Mac OS X, which I have no real experience with. However, it is Unix-like, so some things should be like Linux, with which I am familiar. Try `ps -aux | grep httpd` (if that doesn't work, try `ps-ef | grep httpd`). The first column should show the name of the user which apache runs as; normally this should not be root. Use that username for the chown command. – GreenMatt Feb 10 '11 at 18:52
1

Know this is an old post but I'll add my two cents.

I place my *.py scripts in /Applications/MAMP/cgi-bin

start my scripts with #!/bin/usr/python

                  print "Content-type:text/html \r\n\r\b"

then chmod 755 .py and run it with ./.py from cgi-bin directory

Hope this helps :)