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

Lloyd Kvam pythontutor at venix.com
Mon Dec 15 19:14:27 EST 2003


Barnaby Scott wrote:

> Thanks very much. In fact I like the look of your
> first idea the best - the Location: thing. Even
> shorter and more concise than I had hoped for!
> 
> I think both my problems in fact stem from more lack
> of knowledge of http and webservers than Python, so
> forgive me if this is all slightly off-topic.
> 
> The problem I feared does in fact occur - I used
> urllib to get a page and display it, and sure enough
> the images sources and some links failed to work.
> Because they are relative references, they went from
> the wrong starting point - i.e. not the web directory
> of the page, but cgi-bin where the script lives.

Below is the function I normally use to output an existing HTML file
through a cgi script.  Note that it is all done using normal file IO
and avoids processing the HTML.  urllib is probably "too smart" for
what you are trying to do.  This function should work for HTML files
on your server.

def copy_snippet( filename, out_file=None):
	if out_file is None:
		out_file = sys.stdout
	if os.path.exists( filename):
		snip = open( filename, 'r')
		shutil.copyfileobj( snip, out_file)
		snip.close()

The variable name snip was used because these are usually snippets of
HTML.  (Server side includes may have been a better way to go.)

> 
> As for the web root thing: I can get the DOCUMENT_ROOT
> to display, but this appears to be a totally different
> directory to the one I am interested in! My situation
> is that I have rented space on a server for 3 domains.
> The path I want returned takes the following form:
> 
> /home/myaccountname/webs/www.oneofmydomains.com/htdocs
> 
> The DOCUMENT_ROOT returns instead:
> 
> /usr/local/www/data
> 
> When I look there, the stuff is nothing to do with me
> at all! 

???
I'm not sure what to tell you.  If you run cgi.test() you'll get
a list of all of the environment variables.  Perhaps there is a
better one to use.  Could you have mis-tested in looking up
DOCUMENT_ROOT?  I only have Apache running anywhere that I can check.
All of those locations report the correct DOCUMENT_ROOT for the sites.

-- 
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