Python: using a regular expression to match one line of HTML
- by skylarking
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...