[Tutor] returning a web page (and also path to web root)

Lloyd Kvam pythontutor at venix.com
Mon Dec 15 10:44:16 EST 2003


(I already suggested using the Location: HTTP header, but decided to
add more.)
Barnaby Scott wrote:
> I am trying to write a cgi script in which under rare
> circumstances I want the user to see a particular web
> page (from the same server).
> 
> I am not very experienced and would be very grateful
> for help on the right way to tackle this - without
> knowing a sensible direction to go in I am likely to
> waste huge amounts of time developing a stupid or
> overly-complex solution!
> 
> Presumably if I just read the page in and spat it back
> out to the user, all the relative links, image sources
> etc would be wrong.
Not true.  Your script will operate relative to the cgi-bin directory.
However, if you simply copy a page of HTML back to the browser, the
browser will not know that your CGI sent the HTML as opposed to the
regular HTTPD server.  It will simply process the HTML it receives.
Your script would have trouble resolving the image tags and relative
links, but it does not have to resolve anything within the HTML.
> 
> However I was a little reluctant to start messing with
> the SimpleHTTPServer module - a. because I find it
> daunting and b. I wondered if there was something
> altogether simple and obvious I could do.
> 
> One more quick question: is there an easy way of
> returning the actual path of the webserver's web root?
> (Forgive my terminology if it's not right - I mean the
> root directory as far as the someone accessing the
> site is concerned).
Shell Environment:

DOCUMENT_ROOT
     /var/www/html

The os.environment() method should give you the DOCUMENT_ROOT.
The test method of the cgi module will show the environment provided
by your web server along with any parameters.
To see it yourself, this is all you need:

#!/usr/local/bin/python
import cgi
cgi.test()

Fix the shebang for your situation.
> 
> __________________________________
> Do you Yahoo!?
> New Yahoo! Photos - easier uploading and sharing.
> http://photos.yahoo.com/
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list