Python: using a regular expression to match one line of HTML

Posted by skylarking on Stack Overflow See other posts from Stack Overflow or by skylarking
Published on 2010-03-17T14:48:25Z Indexed on 2010/03/17 14:51 UTC
Read the original article Hit count: 296

Filed under:
|
|
|

This simple Python method I put together just checks to see if Tomcat is running on one of our servers.

import urllib2
import re
import sys

def tomcat_check():

    tomcat_status = urllib2.urlopen('http://10.1.1.20:7880')
    results = tomcat_status.read()
    pattern = re.compile('<body>Tomcat is running...</body>',re.M|re.DOTALL)
    q = pattern.search(results)
    if q == []:
        notify_us()
    else:
         print ("Tomcat appears to be running")
    sys.exit()

If this line is not found :

<body>Tomcat is running...</body>

It calls :

notify_us()

Which uses SMTP to send an email message to myself and another admin that Tomcat is no longer runnning on the server...

I have not used the re module in Python before...so I am assuming there is a better way to do this... I am also open to a more graceful solution with Beautiful Soup ... but haven't used that either..

Just trying to keep this as simple as possible...

© Stack Overflow or respective owner

Related posts about python

Related posts about regex