'print' in a CGI app.

A

Andrew Chalk

In a Python 2.2 app. running under CGI the statements

print "Hello\n"
print "World"

print both words on the same line in IE6. How do I print the second one on a
new line (i.e. respect the \n in the first statement).

Many thanks!
 
I

Irmen de Jong

Andrew said:
In a Python 2.2 app. running under CGI the statements

print "Hello\n"
print "World"

print both words on the same line in IE6. How do I print the second one on a
new line (i.e. respect the \n in the first statement).

Umm. This has nothing to do with Python... but hey :)
Unless your CGI script is set to content-type: text/plain,
you're supposed to print valid HTML.
You'll have more success if you print the following:

print "<html><body>" # begin HTML
print "<pre>" # HTML 'preformatted' block
print "Hello\n"
print "World"
print "</pre>" # end 'preformatted block'
print "</body></html>" # end HTML


--Irmen
 
A

Andrew Chalk

Thanks to you, Timo, Dennis and Irmen. The <BR> trick was what I was using
but I thought I must be missing something.

Regards
 
P

Patrick Lioi

Andrew Chalk said:
In a Python 2.2 app. running under CGI the statements

print "Hello\n"
print "World"

print both words on the same line in IE6. How do I print the second one on a
new line (i.e. respect the \n in the first statement).

Many thanks!

Run the app, then go to "View"->"Source" in the browser. $10 says
they are on separate lines. All browsers are expecting HTML output
from CGI scripts. HTML ignores whitespace. The <br> tag comes in
handy for forcing an endline:

print "Hello<br>"
print "World"
 
A

Andrew Clover

Andrew Chalk said:
print "Hello\n"
print "World"
print both words on the same line in IE6.

This is because IE is treating the page as HTML by default, where
whitespace is not significant. If this is not what you want, set the
'Content-Type' HTTP header to something else, eg.:

#!/usr/bin/python

print 'Content-Type: text/plain; charset=utf-8'
print

print 'Hello'
print 'World'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top