JamieTheTrainie

Untitled

Jun 18th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. Script to check a list of URLs (passed on stdin) for response code, and for response code of the final path in a series of redirects.
  4. Outputs (to stdout) a list of count of a given URL, response code, and if redirected, the final URL and its response code
  5. Optionally, with verbose flag, report on all URL checks on STDERR
  6. Copyright 2013 Jason Antman <[email protected]> all rights reserved
  7. This script is distributed under the terms of the GPLv3, as per the
  8. LICENSE file in this repository.
  9. The canonical version of this script can be found at:
  10. <http://github.com/jantman/misc-scripts/blob/master/check_url_list.py>
  11. """
  12.  
  13. import sys
  14. import urllib2
  15.  
  16. def get_url_nofollow(url):
  17. try:
  18. response = urllib2.urlopen(url)
  19. code = response.getcode()
  20. return code
  21. except urllib2.HTTPError as e:
  22. return e.code
  23. except:
  24. return 0
  25.  
  26. def main():
  27. urls = {}
  28. with open('logurl.txt') as my_file:
  29. for line in my_file:
  30. urls.append(line)
  31. for line in sys.stdin.readlines():
  32. line = line.strip()
  33. if line not in urls:
  34. sys.stderr.write("+ checking URL: %s\n" % line)
  35. urls[line] = {'code': get_url_nofollow(line), 'count': 1}
  36. sys.stderr.write("++ %s\n" % str(urls[line]))
  37. else:
  38. urls[line]['count'] = urls[line]['count'] + 1
  39.  
  40. for url in urls:
  41. if urls[url]['code'] != 200:
  42. print "%d\t%d\t%s" % (urls[url]['count'], urls[url]['code'], url)
  43.  
  44. if __name__ == "__main__":
  45. main()
Advertisement
Add Comment
Please, Sign In to add comment