0

I am trying to capture the stdout when I print on shell. I get output like this:

************* STARTING healthmonitor ***********************

Aug 28, 2014 5:17:58 AM org.rzo.yajsw.os.posix.PosixService getPid
INFO: wrapper pid file: /var/run/wrapper.healthmonitor.pid
Aug 28, 2014 5:17:58 AM org.rzo.yajsw.os.posix.PosixService startProcess
INFO: calling "/usr/bin/java" "-Dwrapper.pidfile=/va/run/wrapper.healthmonitor.pid"              
Aug 28, 2014 5:17:58 AM org.rzo.yajsw.os.posix.PosixService getPid
INFO: wrapper pid file: /var/run/wrapper.healthmonitor.pid
Aug 28, 2014 5:18:00 AM org.rzo.yajsw.os.posix.PosixService getPid
INFO: wrapper pid file: /var/run/wrapper.healthmonitor.pid

However, when I use the same output to print on a webpage, the format gets jumbled like this:

********* STARTING healthmonitor ******************* Aug 28, 2014 4:48:23 AM org.rzo.yajsw.os.posix.PosixService getPid INFO: wrapper pid file: /var/run/wrapper.healthmonitor.pid Aug 28, 2014 4:48:23 AM org.rzo.yajsw.os.posix.PosixService startProcess INFO: calling "/usr/bin/java" "-Dwrapper.pidfile=/var/run/wrapper.healthmonitor.pid" "-Dwrapper.service=true" "-Dwrapper.visible=false" "-Djna_tmpdir=/app/sterling/jsw/healthmonitor/bin/../tmp" "-jar" "/app/sterling/jsw/healthmonitor/wrapper.jar" "-c" "/app/sterling/jsw/healthmonitor/conf/wrapper.conf" Aug 28, 2014 4:48:23 AM org.rzo.yajsw.os.posix.PosixService getPid INFO:

The python script which prints this is called in a java script file using cgi python way of calling. And in the python script, this is how its printed.

import cgi, cgitb

print("Content-type: text/html\n\n")
print """
<html><head></head>
<body>
<br>
"""
print nova.servers.get_console_output(VMID)

print """
</body></html>
"""

The "print nova.servers.get_console_output(VMID)" is the one that produces the console output.

Thanks for any help you can offer.

3
  • Wrap nova.servers.get_console_output(VMID) in a <p> Commented Aug 28, 2014 at 5:56
  • 1
    @shaktimaan, did you mean a <pre>? I don't believe newlines are honored in a <p>, but I know for sure they are in a <pre>. An alternative would be print nova.servers.get_console_output(VMID).replace("\n", "<br>") Commented Aug 28, 2014 at 6:00
  • @sberry Yup, my bad. Meant <pre> Commented Aug 28, 2014 at 6:03

1 Answer 1

1

Newline characters (\n) are not translated to new lines when rendered as HTML. You can use a <pre> tag (preformatted) to allow them to have meaning when being rendered.

...
print """
<html><head></head>
<body>
<br>
<pre>
"""
print nova.servers.get_console_output(VMID)

print """
</pre>
</body></html>
"""

Or you could replace newline characters with a <br>, like so:

print nova.servers.get_console_output(VMID).replace("\n", "<br>")

Either one should do what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

You missed the closing </pre>
sberry, I can't express enough how good it looks now! Thank you so much!! Awesome!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.