Skip to main content
Code Review

Return to Revisions

2 of 3
added 28 characters in body; edited tags
Jamal
  • 35.2k
  • 13
  • 134
  • 238

What is wrong with my code? The recruiter said it didn't meet their bar

I applied for a job and they asked me to write a code with the following requirements:

Get a "toolbar offer" description from http..update.utorrent.com/installoffer.php?offer=conduit Parse the bencoded (see http://en.wikipedia.org/wiki/Bencode) response and extract the URL of the binary and its sha1 hash The hash is contained in the value of the key 'offer_hash' . The binary is in list for the key 'offer_urls' — please use the one prefixed with "http..download.utorrent.com/".

Download the binary and calculate its hash

Verify that the calculated sha1 hash is identical to the one provided in the offer details It is okay to use existing libraries for bencoding, hashing, etc.

Here is the code I wrote in Python:

# Script that verifies the correctness of a toolbar offers
import urllib, bencode , hashlib
from hashlib import sha1
url = "http://update.utorrent.com/installoffer.php?offer=conduit"
filename = "content.txt"
f= urllib.urlretrieve(url, filename)
#Bdecoding the response
with open (str(filename), 'rb') as myfile:
 data=myfile.read()
 decoded = bencode.bdecode(data)
# Returning the list for the key 'offer_urls'
list = decoded['offer_urls']
#print list
# Returning the URL of the binary that is prefixed with "http://download3.utorrent.com/"
length = len (list)
prefix = "http://download3.utorrent.com/"
i= -1
while i < length:
 i = i + 1
 if list[i].startswith(prefix) :
 break
binary= list[i]
print "The URL of the the binary is: " , binary
# Returning the sha1 hash contained in the value of the key 'offer_hash'
 
encrypted_hash = decoded['offer_hash']
sha1_hash1 = encrypted_hash.encode('hex')
print "The sha1 hash contained in the value of the key 'offer_hash' is: " , sha1_hash1 
# Downloading the binary and calculating its hash
urllib.urlretrieve ( binary , "utct2-en-conduit-20130523.exe")
file = "C:\Python27\utct2-en-conduit-20130523.exe"
with open (file, 'rb') as myfile:
 downloaded=myfile.read()
 
k = hashlib.sha1()
k.update(downloaded)
sha1file = k.hexdigest()
print "The sha1 hash of the downloaded binary is: " , sha1file
# Verify that the calculated sha1 hash is identical to the one provided in the offer details
if (sha1file == sha1_hash1) :
 print "Test result = Pass"
else :
 print "Test result = Fail"
user26614
  • 2.2k
  • 2
  • 14
  • 5
lang-py

AltStyle によって変換されたページ (->オリジナル) /