Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/urllib.request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ The :mod:`urllib.request` module defines the following functions:

.. versionchanged:: next
This function calls :func:`socket.gethostbyname` if the URL authority
isn't empty or ``localhost``. If the authority resolves to a local IP
address then it is discarded; otherwise, on Windows a UNC path is
returned (as before), and on other platforms a
isn't empty, ``localhost``, or the machine hostname. If the authority
resolves to a local IP address then it is discarded; otherwise, on
Windows a UNC path is returned (as before), and on other platforms a
:exc:`~urllib.error.URLError` is raised.

.. versionchanged:: next
Expand Down
9 changes: 9 additions & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,8 +1483,17 @@ def open_local_file(self, req):
file_open = open_local_file

def _is_local_authority(authority):
# Compare hostnames
if not authority or authority == 'localhost':
return True
try:
hostname = socket.gethostname()
except (socket.gaierror, AttributeError):
pass
else:
if authority == hostname:
return True
# Compare IP addresses
try:
address = socket.gethostbyname(authority)
except (socket.gaierror, AttributeError):
Expand Down
Loading