#NEWLINE# def urlretrieve_netrc( url , filename = None ) : #NEWLINE# #INDENT# '\n    writes a temporary file with the contents of url. This works\n    similar to urllib2.urlretrieve, but uses netrc as fallback on 401,\n    and has no reporthook or data option. Also urllib2.urlretrieve\n    malfunctions behind proxy, so we avoid it.\n\n    :param url: What to retrieve\n    :param filename: target file (default is basename of url)\n    :returns: (filename, response_headers)\n    :raises: IOError and urlopen errors\n    ' #NEWLINE# fname = None #NEWLINE# fhand = None #NEWLINE# try : #NEWLINE# #INDENT# resp = urlopen_netrc ( url ) #NEWLINE# if filename : #NEWLINE# #INDENT# fhand = open ( filename , 'wb' ) #NEWLINE# fname = filename #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( fdesc , fname ) = tempfile . mkstemp ( ) #NEWLINE# fhand = os . fdopen ( fdesc , 'wb' ) #NEWLINE# #UNINDENT# shutil . copyfileobj ( resp . fp , fhand ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if fhand : #NEWLINE# #INDENT# fhand . close ( ) #NEWLINE# #UNINDENT# #UNINDENT# return ( fname , resp . headers )