TM1 REST API Python Requests ConnectionResetError MaxRetryError ProxyError but JavaScript/jQuery Works
<p>I am trying to run a get request using the TM1 REST API and Python Requests but receive a ConnectionResetError / MaxRetryError / ProxyError. Here is my code:</p>

<pre><code>headers = {'Accept' : 'application/json; charset=utf-8',
               'Content-Type': 'text/plain; charset=utf-8'
          }

login = b64encode(str.encode("{}:{}:{}".format(user, password, namespace))).decode("ascii")

headers['Authorization'] = 'CAMNamespace {}'.format(login)

s = requests.Session()
r = s.get(url+query, headers=headers, proxies=urllib.request.getproxies())
</code></pre>

<p>I've tried the get request with proxy auth / no auth / no proxies but i get the same 3 errors. </p>

<p>For reference, the same url is https and works in JavaScript/jQuery:</p>

<pre><code>function updateTable(){
    $.ajax({
        async:false,
        headers: {
            'Accept' : 'application/json; charset=utf-8',
            'Content-Type': 'text/plain; charset=utf-8',
            'Authorization' : 'CAMNamespace ' + btoa('username' + ':' + 'password' + ':' + 'namespace')
        },
        url: 'url_is_here',
        success: function(data){data_is_here}
}
</code></pre>

<p>Here is the TraceBack:</p>

<pre><code>---------------------------------------------------------------------------
ConnectionResetError                      Traceback (most recent call last)
C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, **response_kw)
    571             if is_new_proxy_conn:
--&gt; 572                 self._prepare_proxy(conn)
    573 

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py in _prepare_proxy(self, conn)
    779 
--&gt; 780         conn.connect()
    781 

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\connection.py in connect(self)
    288                                     server_hostname=hostname,
--&gt; 289                                     ssl_version=resolved_ssl_version)
    290 

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\util\ssl_.py in ssl_wrap_socket(sock, keyfile, certfile, cert_reqs, ca_certs, server_hostname, ssl_version, ciphers, ssl_context, ca_cert_dir)
    307     if HAS_SNI:  # Platform-specific: OpenSSL with enabled SNI
--&gt; 308         return context.wrap_socket(sock, server_hostname=server_hostname)
    309 

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname)
    376                          server_hostname=server_hostname,
--&gt; 377                          _context=self)
    378 

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\ssl.py in __init__(self, sock, keyfile, certfile, server_side, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, family, type, proto, fileno, suppress_ragged_eofs, npn_protocols, ciphers, server_hostname, _context)
    751                         raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
--&gt; 752                     self.do_handshake()
    753 

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\ssl.py in do_handshake(self, block)
    987                 self.settimeout(None)
--&gt; 988             self._sslobj.do_handshake()
    989         finally:

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\ssl.py in do_handshake(self)
    632         """Start the SSL/TLS handshake."""
--&gt; 633         self._sslobj.do_handshake()
    634         if self.context.check_hostname:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

MaxRetryError                             Traceback (most recent call last)
C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    402                     retries=self.max_retries,
--&gt; 403                     timeout=timeout
    404                 )

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, **response_kw)
    622             retries = retries.increment(method, url, error=e, _pool=self,
--&gt; 623                                         _stacktrace=sys.exc_info()[2])
    624             retries.sleep()

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    280         if new_retry.is_exhausted():
--&gt; 281             raise MaxRetryError(_pool, url, error or ResponseError(cause))
    282 

MaxRetryError: HTTPSConnectionPool(host='ip', port=49003): Max retries exceeded with url: /api/v1/Threads (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)))

During handling of the above exception, another exception occurred:

ProxyError                                Traceback (most recent call last)
&lt;ipython-input-36-0d70f21b1422&gt; in &lt;module&gt;()
      1 s = requests.Session()
----&gt; 2 r = s.get(url+query, headers=headers)

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py in get(self, url, **kwargs)
    485 
    486         kwargs.setdefault('allow_redirects', True)
--&gt; 487         return self.request('GET', url, **kwargs)
    488 
    489     def options(self, url, **kwargs):

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    473         }
    474         send_kwargs.update(settings)
--&gt; 475         resp = self.send(prep, **send_kwargs)
    476 
    477         return resp

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
    583 
    584         # Send the request
--&gt; 585         r = adapter.send(request, **kwargs)
    586 
    587         # Total elapsed time of the request (approximately)

C:\Users\vmora\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    463 
    464             if isinstance(e.reason, _ProxyError):
--&gt; 465                 raise ProxyError(e, request=request)
    466 
    467             raise ConnectionError(e, request=request)

ProxyError: HTTPSConnectionPool(host='ip', port=49003): Max retries exceeded with url: /api/v1/Threads (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)))
</code></pre>

<p>A couple of people at work helped me solve my problem. The http and https proxies that I had set in my environment variables were causing the issue. I removed them / restarted / and was then able to query the api.</p>

jquery python rest python-requests cognos-tm1