From 27812eb90248c021d7277a8823dd9c7885f497af Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Fri, 14 Oct 2016 10:17:52 +0100 Subject: [PATCH] Use IDNA2008 to encode hosts. --- requests/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index f5496a96..82be7802 100644 --- a/requests/models.py +++ b/requests/models.py @@ -21,6 +21,7 @@ from .structures import CaseInsensitiveDict from .auth import HTTPBasicAuth from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar +from .packages import idna from .packages.urllib3.fields import RequestField from .packages.urllib3.filepost import encode_multipart_formdata from .packages.urllib3.util import parse_url @@ -363,8 +364,8 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): # Only want to apply IDNA to the hostname try: - host = host.encode('idna').decode('utf-8') - except UnicodeError: + host = idna.encode(host).decode('utf-8') + except (UnicodeError, idna.IDNAError): raise InvalidURL('URL has an invalid label.') # Carefully reconstruct the network location