Advertisement
fduran

Monitor Django site with authentication

Jan 15th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. # www.fduran.com
  2. # script to test/monitor if a Django site using authentication is up
  3.  
  4. # install pip if not already in system
  5. apt-get install python-setuptools python-dev build-essential
  6. easy_install -U pip
  7.  
  8. # install requests
  9. pip install requests
  10.  
  11. # script
  12. import requests
  13.  
  14. # constants
  15. url = 'https://example.com/login/'
  16. username = 'valid user'
  17. password = 'valid password'
  18. expected = 'string in landing authenticated page' # easier if not in login page
  19.  
  20. message = ''
  21. g = requests.get(url)
  22. if g.status_code != 200:
  23.     # alert page down
  24.     message = "ALERT: Page down"
  25. else:
  26.     sessionid = g.cookies['sessionid']
  27.     csrftoken = g.cookies['csrftoken']
  28.     cookies = {'sessionid':sessionid, 'csrftoken':csrftoken}
  29.     payload = {'username':username, 'password':password, 'csrfmiddlewaretoken':csrftoken, 'next':''}
  30.     headers = {'Referer': url}
  31.     p = requests.post(url, cookies=cookies, data=payload, headers=headers)
  32.  
  33.     if p.status_code == 200:
  34.         if p.text.find('Please try again') != -1:
  35.             message = "ALERT: Wrong username or password"
  36.         elif p.text.find(expected) != -1:
  37.             # all good
  38.             #message = "Good, expected content found"
  39.             pass
  40.         else:
  41.             # alert
  42.             message = "ALERT: expected content not found"
  43.     else:
  44.         # alert
  45.         message =  "ALERT: cannot authenticate"
  46.  
  47.  
  48. # alert
  49. if (message):
  50.     # send email, SMS etc
  51.     print message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement