Skip to content

Commit 082495b

Browse files
author
Sky NSS
committed
simplify and unify py2 vs py3
1 parent 0359289 commit 082495b

File tree

2 files changed

+273
-259
lines changed

2 files changed

+273
-259
lines changed

pandas/io/common.py

+16-41
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@
5151

5252

5353
if compat.PY3:
54-
from urllib.request import (urlopen, pathname2url, build_opener,
55-
install_opener,
56-
HTTPPasswordMgrWithDefaultRealm,
57-
HTTPBasicAuthHandler,
58-
HTTPSHandler)
54+
from urllib.request import urlopen, pathname2url, Request
5955
_urlopen = urlopen
6056
from urllib.parse import urlparse as parse_url
6157
from urllib.parse import (uses_relative, uses_netloc, uses_params,
@@ -260,52 +256,31 @@ def file_path_to_url(path):
260256
}
261257

262258

263-
def get_urlopen_args(url_with_uname, auth=None, verify_ssl=True):
264-
def split_auth_from_url(url_with_uname):
265-
o = parse_url(url_with_uname)
266-
usrch = '{}:{}@{}'.format(o.username, o.password, o.hostname)
267-
url_no_usrpwd = url_with_uname.replace(usrch, o.hostname)
268-
return (o.username, o.password), url_no_usrpwd
269-
270-
def get_urlopen_args_py2(uname, pwd, url_no_usrpwd, verify_ssl=True):
271-
req = Request(url_no_usrpwd)
272-
upstr = '{}:{}'.format(uname, pwd)
273-
base64string = base64.encodestring(upstr).replace('\n', '')
274-
req.add_header("Authorization", "Basic {}".format(base64string))
275-
# I hope pandas can support self signed certs too
276-
kwargs = {}
277-
if verify_ssl not in [None, True]:
278-
kwargs['context'] = ssl._create_unverified_context()
279-
return req, kwargs
280-
281-
def get_urlopen_args_py3(uname, pwd, url_no_usrpwd, verify_ssl=True):
282-
# not using urllib.request Request for PY3 because
283-
# this looks like better code from extensibility purpose
284-
passman = HTTPPasswordMgrWithDefaultRealm()
285-
passman.add_password(None, url_no_usrpwd, uname, pwd)
286-
authhandler = HTTPBasicAuthHandler(passman)
287-
if verify_ssl in [None, True]:
288-
opener = build_opener(authhandler)
289-
else:
290-
context = ssl.create_default_context()
291-
context.check_hostname = False
292-
context.verify_mode = ssl.CERT_NONE
293-
opener = build_opener(authhandler, HTTPSHandler(context=context))
294-
install_opener(opener)
295-
return url_no_usrpwd, {}
259+
def split_auth_from_url(url_with_uname):
260+
o = parse_url(url_with_uname)
261+
usrch = '{}:{}@{}'.format(o.username, o.password, o.hostname)
262+
url_no_usrpwd = url_with_uname.replace(usrch, o.hostname)
263+
return (o.username, o.password), url_no_usrpwd
296264

265+
266+
def get_urlopen_args(url_with_uname, auth=None, verify_ssl=True):
297267
uname = pwd = None
298268
if auth and len(auth) == 2:
299269
uname, pwd = auth
300270
if not uname and not pwd:
301271
(uname, pwd), url_no_usrpwd = split_auth_from_url(url_with_uname)
302272
else:
303273
url_no_usrpwd = url_with_uname
274+
upstr = '{}:{}'.format(uname, pwd)
304275
if compat.PY3:
305-
fn = get_urlopen_args_py3
276+
b64str = base64.b64encode(bytes(upstr, 'ascii')).decode('utf-8')
306277
else:
307-
fn = get_urlopen_args_py2
308-
req, kwargs = fn(uname, pwd, url_no_usrpwd, verify_ssl=verify_ssl)
278+
b64str = base64.encodestring(upstr).replace('\n', '')
279+
req = Request(url_no_usrpwd)
280+
req.add_header("Authorization", "Basic {}".format(b64str))
281+
kwargs = {}
282+
if verify_ssl not in [None, True]:
283+
kwargs['context'] = ssl._create_unverified_context()
309284
return req, kwargs
310285

311286

0 commit comments

Comments
 (0)