@@ -1466,7 +1466,7 @@ def get_names(self):
14661466 def open_local_file (self , req ):
14671467 import email .utils
14681468 import mimetypes
1469- localfile = url2pathname (req .full_url , require_scheme = True )
1469+ localfile = url2pathname (req .full_url , require_scheme = True , resolve_netloc = True )
14701470 try :
14711471 stats = os .stat (localfile )
14721472 size = stats .st_size
@@ -1482,7 +1482,7 @@ def open_local_file(self, req):
14821482
14831483 file_open = open_local_file
14841484
1485- def _is_local_authority (authority ):
1485+ def _is_local_authority (authority , resolve ):
14861486 # Compare hostnames
14871487 if not authority or authority == 'localhost' :
14881488 return True
@@ -1494,6 +1494,8 @@ def _is_local_authority(authority):
14941494 if authority == hostname :
14951495 return True
14961496 # Compare IP addresses
1497+ if not resolve :
1498+ return False
14971499 try :
14981500 address = socket .gethostbyname (authority )
14991501 except (socket .gaierror , AttributeError ):
@@ -1643,19 +1645,22 @@ def data_open(self, req):
16431645
16441646# Code move from the old urllib module
16451647
1646- def url2pathname (url , * , require_scheme = False ):
1648+ def url2pathname (url , * , require_scheme = False , resolve_netloc = False ):
16471649 """Convert the given file URL to a local file system path.
16481650
16491651 The 'file:' scheme prefix must be omitted unless *require_scheme*
16501652 is set to true.
1653+
1654+ The URL authority may be resolved with gethostbyname() if
1655+ *resolve_netloc* is set to true.
16511656 """
16521657 if require_scheme :
16531658 scheme , url = _splittype (url )
16541659 if scheme != 'file' :
16551660 raise URLError ("URL is missing a 'file:' scheme" )
16561661 authority , url = _splithost (url )
16571662 if os .name == 'nt' :
1658- if not _is_local_authority (authority ):
1663+ if not _is_local_authority (authority , resolve_netloc ):
16591664 # e.g. file://server/share/file.txt
16601665 url = '//' + authority + url
16611666 elif url [:3 ] == '///' :
@@ -1669,7 +1674,7 @@ def url2pathname(url, *, require_scheme=False):
16691674 # Older URLs use a pipe after a drive letter
16701675 url = url [:1 ] + ':' + url [2 :]
16711676 url = url .replace ('/' , '\\ ' )
1672- elif not _is_local_authority (authority ):
1677+ elif not _is_local_authority (authority , resolve_netloc ):
16731678 raise URLError ("file:// scheme is supported only on localhost" )
16741679 encoding = sys .getfilesystemencoding ()
16751680 errors = sys .getfilesystemencodeerrors ()
0 commit comments