I am trying to download content from a content provider that charges me every time I access a document. The code I have written correctly downloads the content and saves them in a local file but apparently it requests the file twice and I am being double charged. I'm not sure where the file is being requested twice, here is my code:
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)
# use the opener to fetch a URL
file_stream = opener.open(url)
# Open our local file for writing
local_file = open(directory + doc_name, "w+")
#Write to our local file
local_file.write(file_stream.read())
I need to figure out how to read the content while only requesting the document once. Any help would be greatly appreciated.