Advertisement
rfmonk

check_site_with_HEAD_request.py

Jul 7th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. """
  4. check the existance of a web page without downloading
  5. the html content. send a get HEAD request with a browser
  6. client.
  7. """
  8.  
  9. import argparse
  10. import httplib
  11. import urlparse
  12. import re
  13. import urllib
  14.  
  15. DEFAULT_URL = 'http://www.gentoo.org'
  16. HTTP_GOOD_CODES = [httplib.OK, httplib.FOUND, httplib.MOVED_PERMANENTLY]
  17.  
  18. def get_server_status_code(url):
  19.     host, path = urlparse.urlparse(url)[1:3]
  20.     try:
  21.         conn = httplib.HTTPConnection(host)
  22.         conn.request('HEAD', path)
  23.         return conn.getresponse().status
  24.     except StandardError:
  25.         return None
  26.  
  27. if __name__ == '__main__':
  28.     parser = argparse.ArgumentParser(description='Example HEAD Request')
  29.     parser.add_argument('--url', action="store", dest="url", default = DEFAULT_URL)
  30.     given_args = parser.parse_args()
  31.     url = given_args.url
  32.     if get_server_status_code(url) in HTTP_GOOD_CODES:
  33.         print "Server: %s status is OK: " % url
  34.     else:
  35.         print "Server: %s status is NOT OK!" % url
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement