Python cgi script

Y

Yong Wang

Hi, All:
I try to write a CGI script to post network traffic routing info in the web page.
I have successfully obtained traffic info from wraped python with C++ and MYSQL and
SNMP code and output to a file. I need read a file which contain traffic info and
post to web. In CGI script, I use:
file=open('/apps/www/htdocs/internal/nd/output1', 'r')
flag=0
while not flag
aLine = file.readline()
if aLine != "":
print aLine
print '\n'
else:
flag = 1
file.close()
print "</body>"
print "</html>"
The output from print statement above in web can display, but no line separation between different lines.
How can I preserve the orginal line format of the input file (space within a line and space between lines)?
Thank you very much for your help in advance.

I am looking forward to receiving your reply.

Yong
 
C

Christopher T King

file=open('/apps/www/htdocs/internal/nd/output1', 'r')
flag=0
while not flag
aLine = file.readline()
if aLine != "":
print aLine
print '\n'
else:
flag = 1
file.close()
print "</body>"
print "</html>"
The output from print statement above in web can display, but no line
separation between different lines. How can I preserve the orginal line
format of the input file (space within a line and space between lines)?

HTML generally ignores whitespace. You need to either append a break tag
(<br/>) to each line, or enclose the output in <pre>...</pre>. Also don't
forget to start the document with <html><body>.

A couple of other notes:

file.readline() retains the line's newline terminator, and print adds one.
Your code ends up printing 4 newlines per actual line. Just using
'print aLine,' should work (the trailing comma prevents print from adding
a newline).

Assuming you are using a relatively recent version of Python (2.2 or 2.3,
not sure about 2.1), you can rewrite your loop using iteration over the
file object:

for aLine in file:
print aLine,
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top