# Script that verifies the correctness of a toolbar offers
import urllib
import bencode
import hashlib
import contextlib
# it's common practice to put input details like this
# as global constants at the start of your script
URL = "http://update.utorrent.com/installoffer.php?offer=conduit"
PREFIX = "http://download3.utorrent.com/"
# by breaking down your task into functions
# you can make the code easier to follow
def read_bencoded_url(url):
with contextlib.closing(urllib.urlopen(url)) as offer_file:
return bencode.bdecode(offer_file.read())
def find_string_with_prefix(strings, prefix):
for string in strings:
if string.startswith(prefix):
return string
else:
raise Exception("Did not find prefix: {}".format(PREFIXprefix))
# this function is a bit more complicated
# but it avoids saving the file to disk or loading it entirely into memory.
# instead it hashes it 4096 bytes at a time
def hash_of_url(url):
hasher = hashlib.sha1()
with contextlib.closing(urllib.urlopen(url)) as binary_file:
while True:
data = binary_file.read(4096)
if not data:
break
hasher.update(data)
return hasher.hexdigest()
# the actual high level logic just calls the functions
# this avoid obscuring the logic with lower level details
def main():
decoded = read_bencoded_url(URL)
binary = find_string_with_prefix(decoded['offer_urls'], PREFIX)
reported_hash = decoded['offer_hash'].encode('hex')
actual_hash = hash_of_url(binary)
print "The sha1 hash contained in the value of the key 'offer_hash' is: ", reported_hash
print "The sha1 hash of the downloaded binary is: " , actual_hash
if reported_hash == actual_hash:
print "Test result = Pass"
else:
print "Test result = Fail"
if __name__ == '__main__':
main()
# Script that verifies the correctness of a toolbar offers
import urllib
import bencode
import hashlib
import contextlib
# it's common practice to put input details like this
# as global constants at the start of your script
URL = "http://update.utorrent.com/installoffer.php?offer=conduit"
PREFIX = "http://download3.utorrent.com/"
# by breaking down your task into functions
# you can make the code easier to follow
def read_bencoded_url(url):
with contextlib.closing(urllib.urlopen(url)) as offer_file:
return bencode.bdecode(offer_file.read())
def find_string_with_prefix(strings, prefix):
for string in strings:
if string.startswith(prefix):
return string
else:
raise Exception("Did not find prefix: {}".format(PREFIX))
# this function is a bit more complicated
# but it avoids saving the file to disk or loading it entirely into memory.
# instead it hashes it 4096 bytes at a time
def hash_of_url(url):
hasher = hashlib.sha1()
with contextlib.closing(urllib.urlopen(url)) as binary_file:
while True:
data = binary_file.read(4096)
if not data:
break
hasher.update(data)
return hasher.hexdigest()
# the actual high level logic just calls the functions
# this avoid obscuring the logic with lower level details
def main():
decoded = read_bencoded_url(URL)
binary = find_string_with_prefix(decoded['offer_urls'], PREFIX)
reported_hash = decoded['offer_hash'].encode('hex')
actual_hash = hash_of_url(binary)
print "The sha1 hash contained in the value of the key 'offer_hash' is: ", reported_hash
print "The sha1 hash of the downloaded binary is: " , actual_hash
if reported_hash == actual_hash:
print "Test result = Pass"
else:
print "Test result = Fail"
if __name__ == '__main__':
main()
# Script that verifies the correctness of a toolbar offers
import urllib
import bencode
import hashlib
import contextlib
# it's common practice to put input details like this
# as global constants at the start of your script
URL = "http://update.utorrent.com/installoffer.php?offer=conduit"
PREFIX = "http://download3.utorrent.com/"
# by breaking down your task into functions
# you can make the code easier to follow
def read_bencoded_url(url):
with contextlib.closing(urllib.urlopen(url)) as offer_file:
return bencode.bdecode(offer_file.read())
def find_string_with_prefix(strings, prefix):
for string in strings:
if string.startswith(prefix):
return string
else:
raise Exception("Did not find prefix: {}".format(prefix))
# this function is a bit more complicated
# but it avoids saving the file to disk or loading it entirely into memory.
# instead it hashes it 4096 bytes at a time
def hash_of_url(url):
hasher = hashlib.sha1()
with contextlib.closing(urllib.urlopen(url)) as binary_file:
while True:
data = binary_file.read(4096)
if not data:
break
hasher.update(data)
return hasher.hexdigest()
# the actual high level logic just calls the functions
# this avoid obscuring the logic with lower level details
def main():
decoded = read_bencoded_url(URL)
binary = find_string_with_prefix(decoded['offer_urls'], PREFIX)
reported_hash = decoded['offer_hash'].encode('hex')
actual_hash = hash_of_url(binary)
print "The sha1 hash contained in the value of the key 'offer_hash' is: ", reported_hash
print "The sha1 hash of the downloaded binary is: " , actual_hash
if reported_hash == actual_hash:
print "Test result = Pass"
else:
print "Test result = Fail"
if __name__ == '__main__':
main()
Extra space after bencode, suggestbencode
suggests lack of attention to detail.
Use of single letter variable-letter variables suggests lack of attention to clean code.
filename
is already a string, converting. Converting it to a string again suggestsuggests confusion as to what is going on.
DownloadDownloading the file to disk then reading it into memory is poor use of available apisAPIs. You should download the file directly into memory.
Excess whitespace suggests someone is enter-happy.
"Returning" is technically incorrect here. You aren't in a function, so you aren't returning anything. Use of listlist
is a bad name for a variable since itsit's a builtin pythonbuilt-in Python type.
Putting spaces after function calls is odd, and you don't do it consistently. There's also rarely a good reason to store the length of a listlist
.
No space before the =
.
You are using a while
loop when you should be using a for
loop. It is also going to cause an IndexErrorIndexError
, rather than fail gracefully, if the prefix isn't found rather than failing gracefully.
isIs that hash really encrypted?
HardcodedHard-coded filename will be easy to break. You shouldn't even need to do it. The string also contains "\"
which should be escaped, or the whole string should be raw. i.e.
or in most cases you can use the other slashes, even on windowsWindows.
You've put parensparentheses, which are not neccessarynecessary.
Extra space after bencode, suggest lack of attention detail
Use of single letter variable suggests lack of attention to clean code.
filename
is already a string, converting it to a string again suggest confusion as to what is going on.
Download the file to disk then reading it into memory is poor use of available apis. You should download the file directly into memory.
Excess whitespace suggests someone is enter-happy
"Returning" is technically incorrect here. You aren't in a function so you aren't returning anything. Use of list is a bad name for a variable since its a builtin python type.
Putting spaces after function calls is odd, and you don't do it consistently. There's also rarely a good reason to store the length of a list.
No space before the =
You are using a while
loop when you should be using a for
loop. It is also going to cause an IndexError if the prefix isn't found rather than failing gracefully.
is that hash really encrypted?
Hardcoded filename will be easy to break. You shouldn't even need to do it. The string also contains "\"
which should be escaped, or the whole string should be raw. i.e.
or in most cases you can use the other slashes, even on windows.
You've put parens which are not neccessary
Extra space after bencode
suggests lack of attention to detail.
Use of single-letter variables suggests lack of attention to clean code.
filename
is already a string. Converting it to a string again suggests confusion as to what is going on.
Downloading the file to disk then reading it into memory is poor use of available APIs. You should download the file directly into memory.
Excess whitespace suggests someone is enter-happy.
"Returning" is technically incorrect here. You aren't in a function, so you aren't returning anything. Use of list
is a bad name for a variable since it's a built-in Python type.
Putting spaces after function calls is odd, and you don't do it consistently. There's also rarely a good reason to store the length of a list
.
No space before the =
.
You are using a while
loop when you should be using a for
loop. It is also going to cause an IndexError
, rather than fail gracefully, if the prefix isn't found.
Is that hash really encrypted?
Hard-coded filename will be easy to break. You shouldn't even need to do it. The string also contains "\"
which should be escaped, or the whole string should be raw. i.e.
or in most cases you can use the other slashes, even on Windows.
You've put parentheses, which are not necessary.
# Script that verifies the correctness of a toolbar offers
import urllib
import bencode
import hashlib
import contextlib
# it's common practice to put input details like this
# as global constants at the start of your script
URL = "http://update.utorrent.com/installoffer.php?offer=conduit"
PREFIX = "http://download3.utorrent.com/"
# by breaking down your task into functions
# you can make the code easier to follow
def read_bencoded_url(url):
with contextlib.closing(urllib.urlopen(url)) as offer_file:
return bencode.bdecode(offer_file.read())
def find_string_with_prefix(strings, prefix):
for string in strings:
if string.startswith(prefix):
return string
else:
raise Exception("Did not find prefix: {}".format(PREFIX))
# this function is a bit more complicated
# but it avoids saving the file to disk or loading it entirely into memory.
# instead it hashes it 4096 bytes at a time
def hash_of_url(url):
hasher = hashlib.sha1()
with contextlib.closing(urllib.urlopen(binaryurl)) as binary_file:
while True:
data = binary_file.read(4096)
if not data:
break
hasher.update(data)
return hasher.hexdigest()
# the actual high level logic just calls the functions
# this avoid obscuring the logic with lower level details
def main():
decoded = read_bencoded_url(URL)
binary = find_string_with_prefix(decoded['offer_urls'], PREFIX)
reported_hash = decoded['offer_hash'].encode('hex')
actual_hash = hash_of_url(binary)
print "The sha1 hash contained in the value of the key 'offer_hash' is: ", reported_hash
print "The sha1 hash of the downloaded binary is: " , actual_hash
if reported_hash == actual_hash:
print "Test result = Pass"
else:
print "Test result = Fail"
if __name__ == '__main__':
main()
# Script that verifies the correctness of a toolbar offers
import urllib
import bencode
import hashlib
import contextlib
# it's common practice to put input details like this
# as global constants at the start of your script
URL = "http://update.utorrent.com/installoffer.php?offer=conduit"
PREFIX = "http://download3.utorrent.com/"
# by breaking down your task into functions
# you can make the code easier to follow
def read_bencoded_url(url):
with contextlib.closing(urllib.urlopen(url)) as offer_file:
return bencode.bdecode(offer_file.read())
def find_string_with_prefix(strings, prefix):
for string in strings:
if string.startswith(prefix):
return string
else:
raise Exception("Did not find prefix: {}".format(PREFIX))
# this function is a bit more complicated
# but it avoids saving the file to disk or loading it entirely into memory.
# instead it hashes it 4096 bytes at a time
def hash_of_url(url):
hasher = hashlib.sha1()
with contextlib.closing(urllib.urlopen(binary)) as binary_file:
while True:
data = binary_file.read(4096)
if not data:
break
hasher.update(data)
return hasher.hexdigest()
# the actual high level logic just calls the functions
# this avoid obscuring the logic with lower level details
def main():
decoded = read_bencoded_url(URL)
binary = find_string_with_prefix(decoded['offer_urls'], PREFIX)
reported_hash = decoded['offer_hash'].encode('hex')
actual_hash = hash_of_url(binary)
print "The sha1 hash contained in the value of the key 'offer_hash' is: ", reported_hash
print "The sha1 hash of the downloaded binary is: " , actual_hash
if reported_hash == actual_hash:
print "Test result = Pass"
else:
print "Test result = Fail"
if __name__ == '__main__':
main()
# Script that verifies the correctness of a toolbar offers
import urllib
import bencode
import hashlib
import contextlib
# it's common practice to put input details like this
# as global constants at the start of your script
URL = "http://update.utorrent.com/installoffer.php?offer=conduit"
PREFIX = "http://download3.utorrent.com/"
# by breaking down your task into functions
# you can make the code easier to follow
def read_bencoded_url(url):
with contextlib.closing(urllib.urlopen(url)) as offer_file:
return bencode.bdecode(offer_file.read())
def find_string_with_prefix(strings, prefix):
for string in strings:
if string.startswith(prefix):
return string
else:
raise Exception("Did not find prefix: {}".format(PREFIX))
# this function is a bit more complicated
# but it avoids saving the file to disk or loading it entirely into memory.
# instead it hashes it 4096 bytes at a time
def hash_of_url(url):
hasher = hashlib.sha1()
with contextlib.closing(urllib.urlopen(url)) as binary_file:
while True:
data = binary_file.read(4096)
if not data:
break
hasher.update(data)
return hasher.hexdigest()
# the actual high level logic just calls the functions
# this avoid obscuring the logic with lower level details
def main():
decoded = read_bencoded_url(URL)
binary = find_string_with_prefix(decoded['offer_urls'], PREFIX)
reported_hash = decoded['offer_hash'].encode('hex')
actual_hash = hash_of_url(binary)
print "The sha1 hash contained in the value of the key 'offer_hash' is: ", reported_hash
print "The sha1 hash of the downloaded binary is: " , actual_hash
if reported_hash == actual_hash:
print "Test result = Pass"
else:
print "Test result = Fail"
if __name__ == '__main__':
main()