How to use 'urllib urlretrieve python3' in Python

Every line of 'urllib urlretrieve python3' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
109def download_urllib(url, filename):
110 """ Try to donwload via urllib."""
111 print("Trying to Download via urllib from:\n ", url)
112 keep_going = True
113 try:
114 url_res = urllib2.urlopen(url)
115 except (HTTPError, URLError, ssl.CertificateError) as err:
116 print("Error: %s" % err)
117 return False
118 with open(filename, 'wb') as outfile:
119 block_sz = 8192
120 meta = url_res.info()
121 meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all
122 meta_length = meta_func("Content-Length")
123 file_size = None
124 if meta_length:
125 file_size = int(meta_length[0])
126 message = "Downloading: {0}\nBytes: {1}\n".format(url, file_size)
127 dstyle = wx.PD_APP_MODAL | wx.PD_CAN_ABORT | wx.PD_AUTO_HIDE
128 if file_size:
129 progress = wx.ProgressDialog('Downloading', message,
130 maximum=1+file_size/block_sz, style=dstyle)
131 else:
132 progress = wx.ProgressDialog('Downloading', message, style=dstyle)
133
134 file_size_dl = 0
135 while keep_going:
136 read_buffer = url_res.read(block_sz)
137 if not read_buffer:
138 progress.Update(file_size_dl / block_sz, "message+\nDONE!")
139 wx.Sleep(0.2)
140 break
141
142 file_size_dl += len(read_buffer)
143 outfile.write(read_buffer)
144
145 status = "{0:16}".format(file_size_dl)
146 if file_size:
147 status += " [{0:6.2f}%]".format(file_size_dl * 100 / file_size)
148 (keep_going, dummy_skip) = progress.Update(file_size_dl / block_sz,
149 message+status)
150 wx.Sleep(0.08) # Give the GUI some update time
151 progress.Destroy()
201def _download(self, url):
202 return urlopen(url)

Related snippets