def stream_response_to_file(response, path=None):
 pre_opened = False
 fd = None
 filename = None
 if path:
  if isinstance(getattr(path, 'write', None), collections.Callable):
   pre_opened = True
   fd = path
   filename = getattr(fd, 'name', None)
  else:
   fd = open(path, 'wb')
   filename = path
 else:
  header = response.headers['content-disposition']
  i = (header.find('filename=') + len('filename='))
  filename = header[i:]
  fd = open(filename, 'wb')
 for chunk in response.iter_content(chunk_size=512):
  fd.write(chunk)
 if (not pre_opened):
  fd.close()
 return filename

def stream_response_to_file ( response , path = None ) : pre_opened = False fd = None filename = None if path : if isinstance ( getattr ( path , 'write' , None ) , collections . Callable ) : pre_opened = True fd = path filename = getattr ( fd , 'name' , None ) else : fd = open ( path , 'wb' ) filename = path else : header = response . headers [ 'content-disposition' ] i = ( header . find ( 'filename=' ) + len ( 'filename=' ) ) filename = header [ i : ] fd = open ( filename , 'wb' ) for chunk in response . iter_content ( chunk_size = 512 ) : fd . write ( chunk ) if ( not pre_opened ) : fd . close ( ) return filename
stream a response body to the specified file .