Python cgi script

Discussion in 'Python' started by Yong Wang, Aug 5, 2004.

  1. Yong Wang

    Yong Wang Guest

    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
     
    Yong Wang, Aug 5, 2004
    #1
    1. Advertisements

  2. 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,
     
    Christopher T King, Aug 5, 2004
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. praba kar

    Python-cgi or Perl-cgi script doubt

    praba kar, Jul 30, 2005, in forum: Python
    Replies:
    1
    Views:
    846
    Michael Sparks
    Jul 30, 2005
  2. Amir  Michail

    cgi relay for python cgi script

    Amir Michail, Oct 4, 2005, in forum: Python
    Replies:
    7
    Views:
    530
    Michael Ekstrand
    Oct 4, 2005
  3. Muzammil
    Replies:
    1
    Views:
    1,367
    red floyd
    Aug 28, 2008
  4. Gavri Savio Fernandez

    cgi = CGI.new causes the script to hang

    Gavri Savio Fernandez, Nov 30, 2003, in forum: Ruby
    Replies:
    2
    Views:
    298
    Ara.T.Howard
    Dec 1, 2003
  5. kath
    Replies:
    4
    Views:
    863
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page