|
51 | 51 |
|
52 | 52 |
|
53 | 53 | 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 |
59 | 55 | _urlopen = urlopen
|
60 | 56 | from urllib.parse import urlparse as parse_url
|
61 | 57 | from urllib.parse import (uses_relative, uses_netloc, uses_params,
|
@@ -260,52 +256,31 @@ def file_path_to_url(path):
|
260 | 256 | }
|
261 | 257 |
|
262 | 258 |
|
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 |
296 | 264 |
|
| 265 | + |
| 266 | +def get_urlopen_args(url_with_uname, auth=None, verify_ssl=True): |
297 | 267 | uname = pwd = None
|
298 | 268 | if auth and len(auth) == 2:
|
299 | 269 | uname, pwd = auth
|
300 | 270 | if not uname and not pwd:
|
301 | 271 | (uname, pwd), url_no_usrpwd = split_auth_from_url(url_with_uname)
|
302 | 272 | else:
|
303 | 273 | url_no_usrpwd = url_with_uname
|
| 274 | + upstr = '{}:{}'.format(uname, pwd) |
304 | 275 | if compat.PY3:
|
305 |
| - fn = get_urlopen_args_py3 |
| 276 | + b64str = base64.b64encode(bytes(upstr, 'ascii')).decode('utf-8') |
306 | 277 | 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() |
309 | 284 | return req, kwargs
|
310 | 285 |
|
311 | 286 |
|
|
0 commit comments