Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to open a HTML file when the python cgi program is executing?

1,096 views
Skip to first unread message

Ken

unread,
Jun 1, 2002, 2:43:01 AM6/1/02
to
How do I open a HTML file when the cgi program is executing?

The only method I can think of, which doesn't work is this:
<meta HTTP-EQUIV="REFRESH" CONTENT="0; URL=main.html">


Sheila King

unread,
Jun 1, 2002, 3:48:33 AM6/1/02
to
On Sat, 1 Jun 2002 16:43:01 +1000, "Ken" <k...@hotmail.com> wrote in
comp.lang.python in article <ad9q91$vlrsb$1...@ID-49758.news.dfncis.de>:

> How do I open a HTML file when the cgi program is executing?
>
> The only method I can think of, which doesn't work is this:
> <meta HTTP-EQUIV="REFRESH" CONTENT="0; URL=main.html">

What do you mean by "open an HTML file when the cgi program is executing"?

Are you talking about displaying the resulting HTML output.

HTML output from a cgi script is simply printed to standard output.

Here is a very simply cgi script (basically "Hello World"):

=============(begin hello script)===============
#!/usr/bin/env python

print "Content-type: text/html\n"
print "<HTML><HEAD><TITLE>Hello, World!</TITLE></HEAD>"
print "<BODY><H1>HELLO, WORLD!!!</H1></BODY></HTML>"

=============(end hello script)===============

Change the interpreter path

#!/usr/bin/env python

as appropriate for your server.

Save the above as something like hello.py or hello.cgi
upload it to the cgi-bin on your server. Make sure file permissions are set
for executable (this will probably be 777 or 755 depending on your server
configuation). Make sure you upload it in ASCII/TEXT mode.

Then point your browser to

http://www.example.com/cgi-bin/hello.py

where you replace example.com with your actual domain name.

This should display an HTML page with the H1 header saying "Hello, World!!"

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/

Daniel Yoo

unread,
Jun 1, 2002, 4:00:36 AM6/1/02
to
Ken <k...@hotmail.com> wrote:
: How do I open a HTML file when the cgi program is executing?

: The only method I can think of, which doesn't work is this:
: <meta HTTP-EQUIV="REFRESH" CONTENT="0; URL=main.html">

Not quite sure I understand yet.


Do you have a file called 'main.html' that you're trying to display
from your CGI program? I just want to make sure we're work on the
same problem here before babbling. *grin*


Talk to you later!

Ken

unread,
Jun 1, 2002, 4:16:28 AM6/1/02
to

"Daniel Yoo" <dy...@hkn.eecs.berkeley.edu> wrote in message
news:ad9uv4$g2n$2...@agate.berkeley.edu...

Yes, I want to display the content of another html file (main.html in this
example) onto the browser. How do I do that? And also, how do I set target
to which frame to display?

Thanks


Ken

unread,
Jun 1, 2002, 4:18:50 AM6/1/02
to

"Sheila King" <use...@thinkspot.net> wrote in message
news:ad95l...@kserver.org...

No, I mean at some point in the script, I want to get out of it and use
ordinary HTML instead (main.html in this example). I don't mean the "print"
command.

Can anyone help?

Thanks


Jiri Baum

unread,
Jun 1, 2002, 6:12:25 AM6/1/02
to
Ken:

> No, I mean at some point in the script, I want to get out of it and use
> ordinary HTML instead (main.html in this example). I don't mean the
> "print" command.

If you realize this before you've output anything, you can use one of the
redirection / refresh headers, such as "Location".

print """Content-type: text/html
Location: http://replacement-url

<p><a href="http://replacement-url">Click here</a></p>
"""

Theoretically it should be sufficient to just give the Location, but put in
a human-readable redirection just in case.

Otherwise, if you've already started, you must continue. Just open the file
and print it.


As for frames, there's the main document, which decides which frames get
shown; and then the server gets separate requests for each of the frames.
You might want to browse a few pages with telnet to get a feel for how
things work.

Jiri
--
Jiri Baum <ji...@baum.com.au> http://www.csse.monash.edu.au/~jirib
MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools

Ken

unread,
Jun 1, 2002, 8:28:34 AM6/1/02
to
> If you realize this before you've output anything, you can use one of the
> redirection / refresh headers, such as "Location".
>
> print """Content-type: text/html
> Location: http://replacement-url
>
> <p><a href="http://replacement-url">Click here</a></p>

Doesn't seem to work... :(


> """
>
> Theoretically it should be sufficient to just give the Location, but put
in
> a human-readable redirection just in case.
>
> Otherwise, if you've already started, you must continue. Just open the
file
> and print it.
>
>
> As for frames, there's the main document, which decides which frames get
> shown; and then the server gets separate requests for each of the frames.
> You might want to browse a few pages with telnet to get a feel for how
> things work.

Where can I find this document?

Thanks


Sheila King

unread,
Jun 1, 2002, 12:23:27 PM6/1/02
to
On Sat, 1 Jun 2002 18:16:28 +1000, "Ken" <k...@hotmail.com> wrote in
comp.lang.python in article <ad9vo8$utftp$1...@ID-49758.news.dfncis.de>:


> Yes, I want to display the content of another html file (main.html in this
> example) onto the browser. How do I do that? And also, how do I set target
> to which frame to display?

If you are trying to open some file and output it to the browser, then:

f = open('main.html', 'r')
myhtml = f.read()
f.close()
print myhtml

Depending on what else the script has already done. For example, if you are
printing the HTML output, instead of redirecting, you do need to print the
Content-type before printing the contents of main.html:

print "Content-type: text/html\n"

But, if you are redirecting to another URL instead, then omit the line show
above and print this line:

print "Location: http://replacement-url\n"

Sheila King

unread,
Jun 1, 2002, 12:20:06 PM6/1/02
to
On Sat, 1 Jun 2002 22:28:34 +1000, "Ken" <k...@hotmail.com> wrote in
comp.lang.python in article <adaeh8$uum7t$1...@ID-49758.news.dfncis.de>:

> > If you realize this before you've output anything, you can use one of the
> > redirection / refresh headers, such as "Location".
> >
> > print """Content-type: text/html
> > Location: http://replacement-url
> >
> > <p><a href="http://replacement-url">Click here</a></p>
>
> Doesn't seem to work... :(
>
>
> > """

You must remove the line about Content-type.
The Location header will essentially redirect to a different HTML page.
When using the location header, your entire output that is printed should
simply be:

print "Location: http://replacement-url\n"

You should have nothing before this, and there's no point in having
anything after it, since the page is being redirected.

Kragen Sitaker

unread,
Jun 1, 2002, 4:12:02 PM6/1/02
to
Sheila King <use...@thinkspot.net> writes:
> print "Location: http://replacement-url\n"
>
> You should have nothing before this, and there's no point in having
> anything after it, since the page is being redirected.

Some browsers (in particular old versions of Opera) have problems with
redirects every once in a while; that's why redirects traditionally
contain HTML, in case the browser is broken.

Sheila King

unread,
Jun 1, 2002, 4:42:43 PM6/1/02
to
On 01 Jun 2002 16:12:02 -0400, Kragen Sitaker <kra...@pobox.com> wrote in
comp.lang.python in article <83n0uev...@panacea.canonical.org>:

Printing the Location header is not a "redirect" and should contain no
HTML.

Sheila King

unread,
Jun 1, 2002, 4:51:28 PM6/1/02
to
On 01 Jun 2002 16:12:02 -0400, Kragen Sitaker <kra...@pobox.com> wrote in
comp.lang.python in article <83n0uev...@panacea.canonical.org>:

I posted a short, quick, quip of a reply to your message. Although I've
cancelled it now, it will probably still show up on most news servers.

In any case, a Google Groups search does find support for your statements
(which I'm sure is no surprise to you).

As an example:
http://groups.google.com/groups?as_umsgid=DoBG3t.LMv%40world.std.com

Ian Bicking

unread,
Jun 1, 2002, 5:25:13 PM6/1/02
to

Well, that's HTTP -- apache CGI has a few more details. If you don't
include a body, Apache will create one for you (using it's standard
mechanisms and text).

*However* you should send a Status header as well, because if you don't
*and* you don't give a fully qualified URL (i.e., with http://), then
Apache will do a local redirect. In the case of a local redirect, the
browser will not be aware of the redirect, but the output will be
replaced with the target location. Relative links in the target HTML
might be broken because of this.

Ian


Jiri Baum

unread,
Jun 1, 2002, 11:43:50 PM6/1/02
to
Please attribute stuff when you reply, I don't read everything and almost
missed your question.

>> If you realize this before you've output anything, you can use one of
>> the redirection / refresh headers, such as "Location".

>> print """Content-type: text/html
>> Location: http://replacement-url
>>
>> <p><a href="http://replacement-url">Click here</a></p>

Ken:


> Doesn't seem to work... :(

Does any CGI work for you at all?

If you do just a simple: print "Content-type: text/plain\n\nHello there!\n"
does it show up in the browser?

>> As for frames, there's the main document, which decides which frames get
>> shown; and then the server gets separate requests for each of the
>> frames. You might want to browse a few pages with telnet to get a feel
>> for how things work.

> Where can I find this document?

Pick a page you like (preferably fairly small).
telnet www.site.com http
GET /rest-of-url.html

Study the output to see how it was done.


Alternatively, get a book on html. Any book on html should do.

0 new messages