[Tutor] CGI help
Rich Krauter
rmkrauter at yahoo.com
Mon Jan 3 17:31:59 CET 2005
David Holland wrote:
> I have written my first cgi script :-
> The .html code is :-
> <HTML><HEAD><TITLE>
> Friends CGI Demo (Static Screen)
> </TITLE></HEAD>
> <BODY><H3>Friends list for:<I>New User</I></H3>
> <FORM
> ACTION="/home/david/Documents/pyprogramming/friends1.py">
> <B>Enter your name></B>
> <INPUT TYPE=text NAME=person SIZE=15>
> <P><B>How many friends do you have ?<B>
> <INPUT TYPE = radio NAME=how many VALUE="0" CHECKED>0
> <INPUT TYPE = radio NAME=how many VALUE="10">10
> <INPUT TYPE = radio NAME=how many VALUE="25">25
> <INPUT TYPE = radio NAME=how many VALUE="50">50
> <INPUT TYPE = radio NAME=how many VALUE="100">100
> <P><INPUT TYPE=submit></FORM></BODY></HTML>
> The python code is :-
> #!/usr/bin/env python
> import cgi
> reshtml = '''Content-Type: text/html\n
> <HTML><HEAD><TITLE>
> Friends CGI Demo (dynamic screen)
> </TITLE></HEAD>
> <BODY><H3>Friends list for: <I>%s</I></H3>
> Your name is: <B>%s</B><P>
> You have <B>%s</B> friends.
> </BODY></HTML>'''
> form = cgi.FieldStorage()
> who = form['person'].value
> howmany = form['howmany'].value
> print reshtml % (who, who, howmany)
>
>
> Now when I open the page using konqueror and click on
> submit. Instead of the code running, it justs opens
> the .py program in a different window.
> Any ideas ?
>
>
Hi David,
You can use a small python script to provide a cgi web server for testing:
<code>
import BaseHTTPServer
import CGIHTTPServer
def run(server_class=BaseHTTPServer.HTTPServer,
handler_class=CGIHTTPServer.CGIHTTPRequestHandler):
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
run()
</code>
Put this in a file called, say, server.py. In the same directory as
server.py, put a directory called cgi-bin. Put your python cgi scripts
in cgi-bin. Modify the action tag in your html form to point to
/cgi-bin/friends1.py. Click on the server.py icon, or run it from the
command line.
Finally, point your browser to http://127.0.0.1:8000/cgi-bin/friends1.py.
You'll proably want to use apache or some other server if you want to do
more than just test or demo cgi scripts locally, but this can be a
useful alternative for testing simple cgi programs without installing
additional software on your machine.
Good luck.
Rich
More information about the Tutor
mailing list