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

1
  • @Volatil3 chmod +777 test.py worked for me. Commented Oct 5, 2012 at 5:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
@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.
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
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 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
|
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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.