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

Lloyd Kvam pythontutor at venix.com
Wed Dec 17 15:26:49 EST 2003


Barnaby Scott wrote:

> More thanks - but more problems!
> 
> The copy_snippet function won't work for me because I
> actually need the document to be *served* to the
> client, rather than read and printed to stdout.
> Reason: it is likely to be a server-parsed document!
> (In any case surely this will also suffer from the
> relative links problem?)

I guess I don't really understand your environment.  Typically,
a cgi script is running with stdout piped back to the web server.
You need to print the header(s), e.g.:
	print "Content-type: test/html"
print a blank line separator
(I say print because the headers are line oriented and you need
to include line marks to delimit each line.  print will do that
for you.)
and finally print the page contents.

The web server passes this stream of data on to the client browser.

What mechanism are you using to pass data to the client browser?
Are we talking about CGI?

I am not sure what you mean by *served* to the client other than what
I describe above, writing (or printing) through the server to the browser.

> 
> Sadly, Location: isn't working for me either, though
> that looked such a good option. I think the reason is
> that the request to the script is a POST, and the
> 're-request' (is there such a word?) forced by the
> Location header seems to be attempted with another
> POST - and therefore fails (see - I'm learning fast! I
> never knew half this stuff 48 hours ago).

Post means that arguments from the browser to your script
are in the data stream and NOT tagged on the end of the URL.
Python's cgi module insulates you from this difference.

I'd expect that there is something else going on.  The browser
should simply load the page specified by the Location: header.
Are you sure there are no other headers or content to confuse things?

I'd recommend getting trivial little scripts (4 or 5 lines) working
and then joining the pieces together.

> 
> At this point I have been reduced to returning a META
> HTTP-EQUIV="Refresh" tag to the the client. I don't
> like it a) because I believe it is browser-dependent,
> and b) it shows a blank page momentarily (even with
> CONTENT=0)
> 
> Don't worry if you have come to the end of your
> suggestion list! I just wanted to report back with
> thanks and an update.
> 
> 
> 
> --- Lloyd Kvam <pythontutor at venix.com> wrote:
> 
>>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
>>
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> __________________________________
> Do you Yahoo!?
> New Yahoo! Photos - easier uploading and sharing.
> http://photos.yahoo.com/
> 

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