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

Lloyd Kvam pythontutor at venix.com
Mon Dec 15 10:22:43 EST 2003


I believe that a simpler approach would be to use a Location
HTTP header to force the client browser to load the desired page.

A standalone script like this should work:

#! /usr/local/bin/python
# redirect.py
print "Location: http://www.MYDOMAIN.com/some_page.html\n"


Point your browser to:
http://www.MYDOMAIN.com/cgi-bin/redirect.py

Your browser should switch to the alternative web page.

NOTE the \n at the end of the Location: header line.  The headers are
spearated from the body of the output stream by a blank line.  print
will automatically append a newline.  The "\n" forces an additional
line which will be blank.

Prove that this simple script works for you.  Then update the logic
of your CGI script to output this header when appropriate.


Daniel Ehrenberg wrote:
> 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.
>>
>>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.
>>
> 
> Try using the Twisted framework. Among several other
> protocalls, Twisted can act as a web server. It is
> written completely in Python so it's very portable.
> With Twisted's web server, you can use static websites
> or traditional CGI, but it's much more efficient to
> use Twisted's own persistent framework with all of
> these great templating and distributed object tools.
> 
> 
>>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).
> 
> 
> I'm not really sure what you mean. Don't you have to
> define that on the web server software itself and it
> isn't some kind of underlying thing available from
> every program? This might be possible with something
> like mod_python, but not in a more general way.
> 
> Daniel Ehrenberg
> 
> __________________________________
> 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