2

I have the following html button

<td><form id="form2" name="form2" method="post" onclick="../cgi-bin/py/GuestBookEntry.py">
<input type="submit" name="gBookSrib" id="gBookSrib" value="Sribble" />
</form></td>

My python script is

#!/usr/bin/python
print "Content-type: text/html\n\n"
print "<html><body>Hello World!</body></html>

When i click the button i want my serverside script to execute and create a html page. How do i do that?

My webhost service provider is fatcow. I think he runs apache, is there a way to check it though? Yes he supports python cgi script. He mentioned that if i place my 'py' files inside the cgi-bin, it should work, even without the 'she'bang.

Fatcow does not allow custom modules. Only standard python modules. They had not allowed access to my cgi-bin folder by default, so had to call them, and then the below stuff just worked.

2
  • sys.write("Content-type: text/html\r\n\r\n") to fit the spec. Are you familiar with Apache? Commented May 23, 2011 at 17:52
  • @khachik - no am new to apache, my webhoster is fatcow.com, but i think he runs it on apache. Commented May 23, 2011 at 18:03

2 Answers 2

3

You need a webserver that serves Python code.

There are several different possibilities out there.

  • Twisted Web is a webserver written in Python, allowing you to serve content from python with minimal overhead.

  • Apache / ngnix + Django offers a more complete stack to develop a full web application

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

3 Comments

Thanks, but how do i install custom modules. My host provider is someoneelse (fatcow.com) he provides support for python cgi, but how do i print to webpage to see output of my python function on button click
do you have ssh access to your account?
nope i spoke with the customer care, they support only standard Python 2.5 modules. Nothing more. But i got it to work. Thanks
2

Which web server are you using? For apache2 there is very tidy module called apache2-modpython, that takes care of executing server-side python scripts and returning the result to the client.

Using this module your python would look like:

def index (req): return "<html><body>Hello World!</body></html>"

Let's say this script is called test.py and has the URI http://yourserver.tld/test.py, then your form would look like:

<form id="form2" name="form2" method="post" action="test.py">

Inside the entry of your site definition add the following lines

    AddHandler mod_python .py
    PythonHandler mod_python.publisher
    PythonDebug On

2 Comments

where do i add the site definition? .htaccess? i have to see how fatcow (my web host service provider) gives access to htaccess files.
not in htaccess, but in your site definition. By default located in /etc/apache2/sites-enabled.

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.