简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM