11from urllib import request
22from contextlib import contextmanager
3- import os , stat
3+ import os , stat , ssl
4+
5+ ctx = ssl .create_default_context ()
6+ ctx .check_hostname = False
7+ ctx .verify_mode = ssl .CERT_NONE
48
59
610@contextmanager
7- def download_base (url , content_type , ctx = None ):
8- with request .urlopen (url , timeout = 1.0 , context = ctx ) as response :
11+ def download_base (url , content_type , timeout = None ):
12+ with request .urlopen (url , timeout = timeout or 1.0 , context = ctx ) as response :
913 # pprint(response.headers.get_content_type())
1014 if response .headers .get_content_type () != content_type :
1115 raise RuntimeError (f'"{ url } " is not the expected content type.' )
@@ -16,15 +20,15 @@ def download_base(url, content_type, ctx=None):
1620 yield response , nbytes
1721
1822
19- def download_info (url , content_type , ctx = None ):
20- with download_base (url , content_type , ctx ) as (response , nbytes ):
23+ def download_info (url , content_type , timeout = None ):
24+ with download_base (url , content_type , timeout ) as (response , nbytes ):
2125 info = response .read ().decode ("utf-8" )
2226 return info
2327
2428
25- def download_file (outfile , url , content_type , ctx = None , progress = None ):
29+ def download_file (outfile , url , content_type , progress = None , timeout = None ):
2630
27- with download_base (url , content_type , ctx ) as (response , nbytes ):
31+ with download_base (url , content_type , timeout ) as (response , nbytes ):
2832 if progress :
2933 progress (0 , nbytes )
3034
0 commit comments