title: How to check results of wget/urllib2 in Python?
body: <p>I am brand new to Python and am trying to write a monitor to determine whether a Java web app (WAR file) running on <code>localhost</code> (hosted by Apache Tomcat) is running or not. I had earlier devised a script that ran:</p>
  
  <pre><code>ps -aef | grep myWebApp
  </code></pre>
  
  <p>And inspected the results of the <code>grep</code> to see if any process IDs came back in those results.</p>
  
  <p>But it turns out that the host OS only sees the Tomcat process, not any web apps Tomcat is hosting. I next tried to see if Tomcat came with any kind of CLI that I could hit from the terminal, and it looks like the answer is no.</p>
  
  <p>Now, I'm thinking of using <code>wget</code> or maybe even <code>urllib2</code> to determine if my web app is running by having them hit <code>http://localhost:8080/myWebApp</code> and checking the results. Here is my best attempt with <code>wget</code>:</p>
  
  <pre><code>wgetCmd = "wget http://localhost:8080/myWebApp"
  wgetResults = subprocess.check_output([wgetCmd], shell=True, stderr=subprocess.STDOUT)
  
  for line in wgetResults.strip().split('\n'):
      if 'failed' in line:
      print "\nError: myWebApp is not running."
      sys.exit()
  </code></pre>
  
  <p>My thinking here is that, if the web app isn't running, <code>wget</code>'s output should always contain the word "failed" inside of it (at least, from my experience). Unfortunately, when I run this, I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "/home/myUser/mywebapp-mon.py", line 52, in &lt;module&gt;
  main()
  File "/home/myUser/mywebapp-mon.py", line 21, in main
  wgetResults = subprocess.check_output([wgetCmd], shell=True, stderr=subprocess.STDOUT)
  File "/usr/lib/python2.7/subprocess.py", line 544, in check_output
  raise CalledProcessError(retcode, cmd, output=output)
  subprocess.CalledProcessError: Command '['wget http://localhost:8080/myWebApp']' returned non-zero exit status 4
  </code></pre>
  
  <p>Any thoughts as to what's going on here (what the error is)? Also, and more importantly, am I going about this the wrong way? Thanks in advance!</p>