Created
July 31, 2012 16:27
-
-
Save owainlewis/3218254 to your computer and use it in GitHub Desktop.
Dirty QA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2, urlparse | |
# Check a text file of urls to make sure they return a status 200 | |
class StatusChecker(object): | |
""" Check the status codes of multiple URLS """ | |
def __init__(self, url_file="urls.txt"): | |
self.url_file = url_file | |
self.urls = [line.strip() for line in open(self.url_file)] | |
self.url_map = map((lambda x: self.format_url(x)), self.urls) | |
def format_url(self, url): | |
if not url.startswith('http'): | |
url = '%s%s' % ('http://', url) | |
return url | |
def get_status_code(self, url): | |
try: | |
result = None | |
connection = urllib2.urlopen(url) | |
result = connection.getcode() | |
connection.close() | |
return result | |
except urllib2.HTTPError, e: | |
return e.code | |
def check_all(self): | |
for url in self.url_map: | |
code = self.get_status_code(url) | |
print "%s : %s" % (code, url) | |
def main(): | |
StatusChecker().check_all() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment