devgp
devgp

Reputation: 1321

How to print to HTML using python as server side script?

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.

Upvotes: 2

Views: 2896

Answers (2)

Mononofu
Mononofu

Reputation: 902

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

Upvotes: 3

Hyperboreus
Hyperboreus

Reputation: 32459

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

Upvotes: 2

Related Questions