web-checker 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python3
  2. # coding: utf-8
  3. # {{{ Installation notes
  4. # To get it working (SNI support) on debian wheezy:
  5. # - apt-get install python3-openssl python3-minimal
  6. # - apt-get install -t wheezy-backports python3-requests python3-urllib3
  7. # }}}
  8. # {{{ Imports
  9. import sys
  10. import re
  11. import argparse
  12. import requests
  13. import traceback
  14. import time
  15. # }}}
  16. # {{{ Constants
  17. _NAME = "web-checker"
  18. _DESC = "tool to check web page content"
  19. _VERSION = "0.1"
  20. # }}}
  21. # {{{ WebPage
  22. class WebPage(object):
  23. _options = None
  24. _elapsed = 0
  25. def __init__(self, options):
  26. self._options = options
  27. def elapsed(self):
  28. return "%.3f" % (self._elapsed)
  29. def check(self):
  30. start_time = time.time()
  31. r = requests.get(self._options.url,
  32. timeout=self._options.timeout,
  33. verify=not self._options.no_ssl_verify)
  34. self._elapsed = time.time() - start_time
  35. if r.status_code != self._options.status:
  36. raise AssertionError('Wrong HTTP code returned: %d != %d' % (r.status_code, self._options.status))
  37. if self._options.regexp is not None:
  38. try:
  39. exp = re.compile(self._options.regexp)
  40. except:
  41. raise ValueError('Content regex is invalid: %s' % (self._options.regexp))
  42. matched = False
  43. if hasattr(r, 'text') and r.text is not None and exp.findall(r.text) != []:
  44. matched = True
  45. elif hasattr(r, 'content') and r.content is not None and exp.findall(r.content) != []:
  46. matched = True
  47. if not matched:
  48. raise AssertionError('Web page content do not match regexp: %s' % (self._options.regexp))
  49. # }}}
  50. # {{{ main
  51. def main():
  52. parser = argparse.ArgumentParser(prog=_NAME, description=_DESC)
  53. parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + _VERSION )
  54. parser.add_argument('-d', '--debug', dest='debug', default=False, action='store_true',
  55. help='Print additional debug informations: traceback, etc. (default: no)')
  56. parser.add_argument('-t', '--timeout', dest='timeout', default=5, type=int, help='Timeout in seconds')
  57. parser.add_argument('-s', '--status', dest='status', default=200, type=int, help='Expected HTTP status')
  58. parser.add_argument('-r', '--regexp', dest='regexp', default=None, help='Regexp matching for web page content')
  59. parser.add_argument('-n', '--no-ssl-verify', dest='no_ssl_verify', default=False, action='store_true', help='Disable SSL certificate verification')
  60. parser.add_argument('-E', '--elapsed', dest='elapsed', default=False, action='store_true', help='Return elapsed time instead of status')
  61. parser.add_argument('url')
  62. options = parser.parse_args()
  63. result = None
  64. try:
  65. w = WebPage(options)
  66. w.check()
  67. result = w.elapsed() if options.elapsed else 0
  68. except Exception as e:
  69. result = '' if options.elapsed else 1
  70. if options.debug:
  71. print('Exception raised: %s' % (e))
  72. traceback.print_exc(file=sys.stderr)
  73. sys.exit(-1)
  74. print(result)
  75. # }}}
  76. # {{{ __main__
  77. if __name__ == "__main__":
  78. main()
  79. # }}}
  80. # vim: foldmethod=marker foldlevel=0 foldenable