Skip to content

Instantly share code, notes, and snippets.

@pjperez
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjperez/d301ed45bc36a754fbe6 to your computer and use it in GitHub Desktop.
Save pjperez/d301ed45bc36a754fbe6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
from bs4 import BeautifulSoup
import requests
import re
import json
try:
url = sys.argv[1]
string = sys.argv[2]
if sys.argv[3] == "False":
allowredirect = False
else:
allowredirect = sys.argv[3]
except:
print "Syntax Error!\n\n"
print "Iz u all rite? Usage:"
print "# allrite <url> <\"string to match\"> <followredirects?>"
print "e.g.: # allrite http://www.fluffcomputing.com \"cloud\" False\n"
sys.exit()
results = {0:"Unable to connect", 1:"String not found", 2:"Unexpected status code ", 3:"It\'s alive!", 301: "301 Redirect", 302: "302 Redirect", 404: "404 Object not found", 200: "200 OK"}
def getSoup(url, allowredirect):
try:
r = requests.get(url, allow_redirects=allowredirect)
except:
result_index = 0
print results[result_index]
sys.exit()
htmlsite = r.text
soup = BeautifulSoup(htmlsite)
return r, soup
def healthCheck(string, r, soup):
if r.status_code == 200:
if soup.find(text = re.compile(string)) >= 1:
result_index = 3
return result_index, r.status_code
else:
result_index = 1
return result_index, r.status_code
else:
result_index = 2
return result_index, r.status_code
def runCheck():
r, soup = getSoup(url, allowredirect)
index = healthCheck(string, r, soup)
print json.dumps({"url":url,"reason":results[index[0]],"code":str(results[index[1]])})
runCheck()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment