Difference between plain print, and print with format string?

Steve Holden sholden at holdenweb.com
Thu Jan 17 09:04:34 EST 2002


--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/


"Walter Dörwald" <walter at livinglogic.de> wrote in message
news:mailman.1011266366.2484.python-list at python.org...
> Chris wrote:
>
> > I noticed that in my python cgi script, the following comes up in
> > Netscape 4.7 as plain text:
> >
> > --------------------
> > def htmlHeader():
> >   return "Content-type: text/html\n"
> >
> > ...
> >
> > print htmlHeader()
> >
> > print <html tags and stuff here...>
> > ----------------------
> >
> > This works in IE 5.5, but just dumps the plain html in Netscape 4.7.
> > Choosing View | Page Info shows the "File MIME Type", according to
> > Netscape, as "text/plain".  Changing the line above to the following
> > fixes the Netscape issue:
> >
> >
> > print "%s" % htmlHeader()
>
>
> This adds a linefeed and terminates the headers

No, it doesn't:

>>> def HTMLHeader():
...  return "Content-Type: text/html\n"
...
>>> print HTMLHeader()
Content-Type: text/html

>>> print "%s" % HTMLHeader()
Content-Type: text/html

>>>
Absolutely no difference there at all, as theory would suggest.

>                                                  If you change
> your function to
>
> def htmlHeader():
>     return "Content-type: text/html\n\n"
>
> it will work.
>
Care is needed when formulating web server HTTP responses. Although HTML is
a very forgiving format and an extra newline won't make any difference, it
can be catastrophic if you are trying to send binary data, which the client
will interpret as beginning immediately after the blank line following the
headers.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list