[Tutor] How do you simulate a CGI script locally?

Magnus Lycka magnus@thinkware.se
Fri Dec 13 10:28:01 2002


>On Thursday 12 December 2002 23:08, Alfred Milgrom wrote:
> > I guess this is a pretty stupid question

No, it's not.

> > I want to develop a CGI script, and I'd like to test it locally (I mean on
> > my own computer, running Windows), rather than uploading the file to the
> > server, testing it, revising it, and so on.
> >
> > My question is: how do I make my Python program behave like a CGI script,
> > where print statements end up shown in the browser window. If I point my
> > browser at my CGI test file, it runs, but the output goes to the DOS box
> > instead.
At 23:19 2002-12-12 -0800, Sean 'Shaleh' Perry wrote:
>a) install a web server on your local machine

If you don't have a web server, you can use this little one:
#############
# mywebserver.py
import BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer

class myRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
     def is_executable(self, path):
         return self.is_python(path)

if __name__ == '__main__':
     SimpleHTTPServer.test(myRequestHandler, BaseHTTPServer.HTTPServer)
#############

Put "mywebserver.py" in a directory of your liking.

Put your python CGI script (e.g. script.py) in a subdirectory
called cgi-bin. (Script name must end .py etc, or you have to
modify "is_ececutable" above.

Run "mywebserver.py"

Point your browser to "http://localhost:8000/cgi-bin/script.py

Enjoy!

>b) in the end a cgi is simply a program which gets some input and prints some
>output.  You can test these programs from the command line, especially easy
>if you install the cygwin tools.

Agreed.

But even without cygwin, it's not so diffictult to run

python script.py > test.html

And then you can look at the HTML with your browser.

Again, I'd like to push for using "cgitb". It's very helpful.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se