# Buggy code
def add_username_and_pass_to_url(url, username, passwd):
    url_obj = parse_url(url)
    url_obj.auth = username + ":" + quote(passwd, "")
    return url_obj.url


# Traceback (OSError)
C:\>conda search scikit-learn

https proxy username: john.doe
Password:
An unexpected error has occurred.
Please consider posting the following information to the
conda GitHub issue tracker at:

https://github.com/conda/conda/issues



Current conda install:

platform : win-64
conda version : 4.2.7
conda is private : False
conda-env version : 4.2.7
conda-build version : not installed
python version : 3.5.2.final.0
requests version : 2.11.1
root environment : C:\Miniconda3  (writable)
default environment : C:\Miniconda3
envs directories : C:\Miniconda3\envs
package cache : C:\Miniconda3\pkgs
channel URLs : https://repo.continuum.io/pkgs/free/win-64/
https://repo.continuum.io/pkgs/free/noarch/
https://repo.continuum.io/pkgs/pro/win-64/
https://repo.continuum.io/pkgs/pro/noarch/
https://repo.continuum.io/pkgs/msys2/win-64/
https://repo.continuum.io/pkgs/msys2/noarch/
config file : C:\Users\john.doe\.condarc
offline mode : False



`$ C:\Miniconda3\Scripts\conda-script.py search scikit-learn`




Traceback (most recent call last):
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 589, in urlopen
self._prepare_proxy(conn)
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 797, in _prepare_proxy
conn.connect()
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\connection.py", line 267, in connect
self._tunnel()
File "C:\Miniconda3\lib\http\client.py", line 832, in _tunnel
message.strip()))
OSError: Tunnel connection failed: 407 Proxy Authentication Required

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Miniconda3\lib\site-packages\requests\adapters.py", line 423, in send
timeout=timeout
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 668, in urlopen
release_conn=release_conn, **response_kw)
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 668, in urlopen
release_conn=release_conn, **response_kw)
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 668, in urlopen
release_conn=release_conn, **response_kw)
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 640, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Miniconda3\lib\site-packages\requests\packages\urllib3\util\retry.py", line 287, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='repo.continuum.io', port=443): Max retries exceeded with url: /pkgs/free/win-64/repodata.json.bz2 (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Miniconda3\lib\site-packages\conda\fetch.py", line 112, in fetch_repodata
timeout=(3.05, 60))
File "C:\Miniconda3\lib\site-packages\requests\sessions.py", line 488, in get
return self.request('GET', url, **kwargs)
File "C:\Miniconda3\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "C:\Miniconda3\lib\site-packages\requests\sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "C:\Miniconda3\lib\site-packages\requests\adapters.py", line 485, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='repo.continuum.io', port=443): Max retries exceeded with url: /pkgs/free/win-64/repodata.json.bz2 (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Miniconda3\lib\site-packages\conda\exceptions.py", line 472, in conda_exception_handler
return_value = func(*args, **kwargs)
File "C:\Miniconda3\lib\site-packages\conda\cli\main.py", line 144, in _main
exit_code = args.func(args, p)
File "C:\Miniconda3\lib\site-packages\conda\cli\main_search.py", line 126, in execute
execute_search(args, parser)
File "C:\Miniconda3\lib\site-packages\conda\cli\main_search.py", line 174, in execute_search
unknown=args.unknown)
File "C:\Miniconda3\lib\site-packages\conda\api.py", line 24, in get_index
index = fetch_index(channel_urls, use_cache=use_cache, unknown=unknown)
File "C:\Miniconda3\lib\site-packages\conda\fetch.py", line 287, in fetch_index
repodatas = [(u, f.result()) for u, f in zip(urls, futures)]
File "C:\Miniconda3\lib\site-packages\conda\fetch.py", line 287, in <listcomp>
repodatas = [(u, f.result()) for u, f in zip(urls, futures)]
File "C:\Miniconda3\lib\concurrent\futures\_base.py", line 405, in result
return self.__get_result()
File "C:\Miniconda3\lib\concurrent\futures\_base.py", line 357, in __get_result
raise self._exception
File "C:\Miniconda3\lib\concurrent\futures\thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "C:\Miniconda3\lib\site-packages\conda\fetch.py", line 70, in func
res = f(*args, **kwargs)
File "C:\Miniconda3\lib\site-packages\conda\fetch.py", line 178, in fetch_repodata
handle_proxy_407(url, session)
File "C:\Miniconda3\lib\site-packages\conda\fetch.py", line 212, in handle_proxy_407
session.proxies[scheme], username, passwd)
File "C:\Miniconda3\lib\site-packages\conda\common\url.py", line 46, in add_username_and_pass_to_url
url_obj.auth = username + ':' + quote(passwd, '')
AttributeError: can't set attribute


C:\>

# Fixed code
def add_username_and_pass_to_url(url, username, passwd):
    url_parts = parse_url(url)._asdict()
    url_parts["auth"] = username + ":" + quote(passwd, "")
    return Url(**url_parts).url