@@ -130,22 +130,25 @@ def download(url, out_file, resume=True, method=None, post_data=None,
130130 when setting resume = True, it's user's responsibility to ensure the file
131131 on disk is the same version as the file on server.
132132 """
133- # TODO: add more configurations from kwargs
134- c = pycurl .Curl ()
135- c .setopt (c .FOLLOWLOCATION , True )
136- c .setopt (c .URL , url )
137- c .setopt (c .TIMEOUT , timeout )
138- c .setopt (c .CAINFO , certifi .where ()) # For HTTPS
139- c .setopt (c .USERAGENT , "ddf_utils/1.0" )
140- c .setopt (c .COOKIEFILE , "" )
141- # c.setopt(c.VERBOSE, True)
142- if progress_bar :
143- c .setopt (c .NOPROGRESS , False )
144- if post_data :
145- c .setopt (c .POSTFIELDS , urlencode (post_data ))
133+ def prepare_curl ():
134+ # TODO: add more configurations from kwargs
135+ c = pycurl .Curl ()
136+ c .setopt (c .FOLLOWLOCATION , True )
137+ c .setopt (c .URL , url )
138+ c .setopt (c .TIMEOUT , timeout )
139+ c .setopt (c .CAINFO , certifi .where ()) # For HTTPS
140+ c .setopt (c .USERAGENT , "ddf_utils/1.0" )
141+ c .setopt (c .COOKIEFILE , "" )
142+ # c.setopt(c.VERBOSE, True)
143+ if progress_bar :
144+ c .setopt (c .NOPROGRESS , False )
145+ if post_data :
146+ c .setopt (c .POSTFIELDS , urlencode (post_data ))
147+ return c
146148
147149 @retry (times = retry_times , backoff = backoff , exceptions = (Exception ))
148150 def run (resume ):
151+ c = prepare_curl ()
149152 acceptor = DownloadAcceptor (c , out_file , resume )
150153 if resume and osp .exists (out_file ):
151154 first_byte = osp .getsize (out_file )
@@ -155,10 +158,11 @@ def run(resume):
155158 if c .getinfo (c .HTTP_CODE ) == 416 :
156159 print ("the http status code is 416, possibly the download was completed." )
157160 print ("if you believe it's not completed, please remove the file and try again." )
161+ c .close ()
158162 return
159163
160164 run (resume )
161- c . close ()
165+
162166
163167
164168def download_2 (url , out_file , session = None , resume = True , method = "GET" , post_data = None ,
0 commit comments