Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python cgi script

Reply
Thread Tools

Python cgi script

 
 
Yong Wang
Guest
Posts: n/a
 
      08-05-2004
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

 
Reply With Quote
 
 
 
 
Christopher T King
Guest
Posts: n/a
 
      08-05-2004
On Thu, 5 Aug 2004, Yong Wang wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error when python script run as cgi script sophie_newbie Python 3 10-22-2007 05:39 PM
what's wrong calling a Perl/CGI script in Perl/CGI script under Tomcat server? kath Perl Misc 4 04-09-2007 09:21 PM
run a script and supply commands from a python cgi script moishyyehuda@gmail.com Python 1 12-14-2006 05:16 PM
cgi relay for python cgi script Amir Michail Python 7 10-04-2005 07:11 PM
Python-cgi or Perl-cgi script doubt praba kar Python 1 07-30-2005 08:25 AM



Advertisments