[Tutor] THE CGI SERVER SCRIPT

Kirk Bailey idiot1 at netzero.net
Tue Sep 9 02:03:08 EDT 2003


I will be glad to post it here, NOW:

#!C:\Python22\pythonw.exe
import CGIHTTPServer, BaseHTTPServer
httpd=BaseHTTPServer.HTTPServer(('',80), CGIHTTPServer.CGIHTTPRequestHandler)
httpd.serve_forever()
# this is a small but serviceable cgi server for your pc!

That's it. BTW, it is Steve Holden's work, and he deserve the praise, not I. He 
wrote 'Python Web Programming'. It is there on page 251, but has a bug, which I 
alerted hom to, and he corrected. This version in this posting WORKS.

To have it work on a different port number, change '80' to 'foo', where foo is 
the number of the port you wish to use. Teh default port used for http 
transferrs is 80. Your local pc will respond to 'http://localhost/' or to 
'http://127.0.0.1/', just like a regular server- because it IS a server.

You may have to modify the #! line to point at pythonw.exe if it is not a 
standard install of IDLE in your pc.

You can create an icon on the desktop to point to it. The target should be as:
C:\Python22\pythonw.exe C:\PYTHON22\cgiserver.py
and it should start in
C:\Python22
and it should run minimized.

DO NOT run this while online unless you have a firewall in operation, and even 
then it is more secure if you use a high numbered non standard port- above 1024 
at least. To make it simple, try 8080. To talk to the server with port 8080, 
your link looks like:
http://localhost:8080/

Hope this helps.

As for cgi writing, this is the list to discuss it. A server has to be told with 
a special header what is coming along, then you output normally. For example, 
here is a simple helloworld cgi program in python:

#!C:\Python22\pythonw.exe
print 'Content-Type: text/html\n'	# notice the extra newline-important!
print '<html><head><title>Hello world</title></head>'
print '<body bgcolor="FFFFFF" text="000000" links="0000FF">'
print '<center><h1>Hello World!</h1>'
print '<P>'
print 'That's all.<P>'
print '</body></html>'

In C:\python22, create the 'cgi-bin' directory, and place it there.

In c:\Python22, create 'index.html' with the usual stuff, and a link to the 
script.  This should look like:
<a href="./cgi-bin/helloworld.py">Hello World Script</a>

If the server is working, entering
http://localhost:8080/index.html
will result in the page displaying. Clicking the link should result in the page 
  generated by the script being displayed.

What fun  hwe can have testing scripts locally with a local offline server! 
THANKS, STEVE!
Ole Jensen wrote:

> Hi Kirk
> I don't really have any idea as to how I make CGI scripts(but I would like
> to), and if I would be able to experiment with making cgi using your script,
> without finding a webspace that supports cgi and all that, I would like a
> copy very much.
> 
> Where can I download it, or will you send it?
> 
> Ole
> 
> ----- Original Message -----
> From: "Kirk Bailey" <idiot1 at netzero.net>
> To: "Tutor" <tutor at python.org>
> Sent: Monday, September 08, 2003 4:06 AM
> Subject: [Tutor] THE CGI SERVER SCRIPT
> 
> 
> 
>>I am using the cgi server script on my desktop pc so I can devlop scripts
>>without going online. IT WORKS GREAT!
>>
>>I  have it living on port 8000, although I tried it out on port 80- either
> 
> way,
> 
>>it's cherry. And as I am running python 2.2.1, I can take advantage of
> 
> cgitb,
> 
>>which is startlingly novel after 2 years of working via ssh (well, PuTTY)
> 
> to get
> 
>>into Critter, who is still running 1.6.2.
>>
>>Anyone want the corrected script, email me. If there is enough intrest, I
> 
> will
> 
>>post it on the tinylist.org site.
>>
>>
>>--
>>
>>--
>>
>>end
>>
>>Cheers!
>>         Kirk D Bailey
>>
>>  +                              think                                +
>>   http://www.howlermonkey.net  +-----+        http://www.tinylist.org
>>   http://www.listville.net     | BOX |  http://www.sacredelectron.org
>>   Thou art free"-ERIS          +-----+     'Got a light?'-Promethieus
>>  +                              think                                +
>>
>>Fnord.
>>
>>
>>
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
> 
> 
> 
> 

-- 

-- 

end

Cheers!
         Kirk D Bailey

  +                              think                                +
   http://www.howlermonkey.net  +-----+        http://www.tinylist.org
   http://www.listville.net     | BOX |  http://www.sacredelectron.org
   Thou art free"-ERIS          +-----+     'Got a light?'-Promethieus
  +                              think                                +

Fnord.




More information about the Tutor mailing list