displaying properly formatted output of ipconfig.exe

Discussion in 'Python' started by Joe Flynt, Nov 8, 2003.

  1. Joe Flynt

    Joe Flynt Guest

    I'm try to display the output of ipconfig.exe to the web browser
    using:
    Apache/2.0.48 (Win32) mod_python/3.1.2b Python/2.3.2

    but when I view http://server/cgi-bin/test.py i get the following
    format of output:
    ['\r\n', 'Windows IP Configuration\r\n', '\r\n',
    etc.

    How do I get it to display the same output as if I had executed the
    program in cmd.exe?

    This is the source of test.py:

    #!C:\Python23\python.exe
    import os
    print "Content-type: text/html\r\n\r\n"
    cmdpipe = os.popen("ipconfig","r")
    lines = cmdpipe.readlines()
    print lines
     
    Joe Flynt, Nov 8, 2003
    #1
    1. Advertisements

  2. You don't want to print a list of strings, you want to print each string in
    a list....

    lines = cmdpipe.readlines()
    - print lines
    + for line in lines:
    + print line
     
    Francis Avila, Nov 8, 2003
    #2
    1. Advertisements

  3. .
    .
    .
    OR perhaps you want simply to print the output:
    - lines = cmdpipe.readlines()
    - for line in lines:
    - print line
    + print cmdpipe.read()

    (There are other alternatives; these are likely to be of most interest.)
     
    Cameron Laird, Nov 9, 2003
    #3
  4. Joe Flynt

    Mark Hahn Guest

    OR, perhaps you want it to look right in a web page:

    #!C:\Python23\python.exe
    import os
    print "Content-type: text/html\r\n\r\n"
    cmdpipe = os.popen("ipconfig","r")
    print '<html><head><title>ipconfig</title></head><body>'
    lines = cmdpipe.readlines()
    for line in lines:
    print line,'<br>'
    print '</body></html>'
     
    Mark Hahn, Nov 9, 2003
    #4
  5. Actually, I just realized a subtle problem with using the print statement in
    (almost) all these examples (including my own). Since the output already
    includes newlines, and print appends a newline, you'll end up with doubled
    newlines. Perhaps just used the write() method of the file object of
    interest? Or, you could append a comma to all your print statements
    (although this is bound to cause maintenance problems later).
     
    Francis Avila, Nov 9, 2003
    #5
  6. Francis Avila fed this fish to the penguins on Sunday 09 November 2003
    10:12 am:
    Doesn't matter... if the output is going to a web browser as HTML, new
    lines are not displayed -- they are white space that the browser
    ignores.

    To get each line on its own needs the newlines to be replaced with
    <br> tags.

    --
     
    Dennis Lee Bieber, Nov 9, 2003
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.