Skip to content

Instantly share code, notes, and snippets.

@ninjix
Created October 21, 2014 02:11
Show Gist options
  • Save ninjix/f13763efe8ec2fe2a1fa to your computer and use it in GitHub Desktop.
Save ninjix/f13763efe8ec2fe2a1fa to your computer and use it in GitHub Desktop.
A sha1sum check for file URL
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" File download check """
__author__ = "Clayton Kramer"
__copyright__ = "Copyright 2014"
__credits__ = ["Clayton Kramer"]
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Rob Knight"
__email__ = "clayton.kramer <at> gmail.com"
__status__ = "Production"
import urllib2
import hashlib
import optparse
def url_sha1sum(url):
"""
Download a file from a URL and return a sha1sum of it.
:param url:
"""
response = urllib2.urlopen(url)
pdf = response.read()
response.close()
return hashlib.sha1(pdf).hexdigest()
if __name__ == '__main__':
desc = 'This is a file download check tool.'
parser = optparse.OptionParser(
description=desc,
version='%prog {}'.format(__version__),
usage='Usage: %prog <options> url'
)
parser.add_option(
'-t', '--test', action='store',
dest='test', help='sha1sum test value'
)
parser.add_option(
'-n', '--noop', action='store_true',
dest='noop', help='No sha1 check. Returns sha1sum of URL.'
)
opts, args = parser.parse_args()
if len(args) < 1:
parser.print_help()
exit(-1)
file_url = None
try:
file_url = args[0]
except IndexError:
parser.error('URL argument missing')
exit(-1)
if opts.noop:
print url_sha1sum(file_url)
exit()
if opts.test is None:
print "Error: No sha1sum test string provided."
parser.print_help()
exit(-1)
else:
if url_sha1sum(file_url) == opts.test:
print 1
else:
print 0
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment