[Tutor] getting binary file from website with custom header

Alex Hall mehgcap at gmail.com
Sat Jan 29 01:14:01 CET 2011


Hello,
I am continuing to work on that api wrapper... I never realized how
little I know about urllib/urllib2! The idea of downloading from the
api is pretty easy: give it a url and a password and it gives you the
book. Here is a quote from the api documentation:
In addition the MD5 hash of the end user password must be passed in
the request via a "X-password" HTTP header.

Here is what I am doing. I use hashlib.md5(password).hexdigest() to
get the md5 of the password. "base" is just the base url, and
"destination" is just a local path. If it matters, this is an https
url.
 user=urllib.quote(user) #user is an email address, so make it useable in a url
  req=urllib2.Request(base+"download/for/"+user+"/content/"+str(id),
None, {"X-password":password})
  try:
   book=urllib2.urlopen(req)
   local=open(destination+str(id), "w") #name the file
   local.write(book.read()) #save the blob to the local file
   local.close()
  except urllib2.HTTPError, e:
   print "HTTP error "+str(e.code)
  except urllib2.URLError, e:
   print "URL error: "+e.reason

I keep getting an error 403, which the api defines as a bad login
attempt. I am sure my password is right, though, so while I
investigate, I thought I would check that I am not only going about
this http header thing right but also getting the binary object right.
I am following an example I found pretty closely.

-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list