For a deeper look into our DataScope Select SOAP API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials

question

Upvotes
Accepted
5 2 1 3

I cannot connect and access DSS REST API through Python code

Hi,

I am trying to use Python to access DSS REST API and I got the result all time correctly. But in recent days, I got the error about SSLError. I am not sure what happened. Could you please help to see whether my account issue or DSS REST API issue?

Here is the code:

import requests, json
from requests import Request, Session
from collections import OrderedDict
urlGetToken = 'https://hosted.datascopeapi.reuters.com/RestApi/v1/Authentication/RequestToken'
header1 = {'Content-Type': 'application/json'}
tokenRequestBody = json.dumps({'Credentials': {'Password': '<password>', 'Username': '<username>'}})
response = requests.post(urlGetToken, tokenRequestBody, headers = header1)
statusCode = response.status_code
if statusCode != 200:
    print('ERROR: Get Token failed with HTTP status code: ' + str(statusCode))
    sys.exit(-1)
else:
    result = response.json()
    token = result['value']
    print(token)

Here is the error message thrown from Python

---------------------------------------------------------------------------
SysCallError                              Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname)
    440             try:
--> 441cnx.do_handshake()
    442             except OpenSSL.SSL.WantReadError:

/anaconda3/lib/python3.6/site-packages/OpenSSL/SSL.py in do_handshake(self)
   1805         result = _lib.SSL_do_handshake(self._ssl)
-> 1806self._raise_ssl_error(self._ssl, result)
   1807 

/anaconda3/lib/python3.6/site-packages/OpenSSL/SSL.py in _raise_ssl_error(self, ssl, result)
   1537                     if errno != 0:
-> 1538raise SysCallError(errno, errorcode.get(errno))
   1539                 raise SysCallError(-1, "Unexpected EOF")

SysCallError: (54, 'ECONNRESET')

During handling of the above exception, another exception occurred:

SSLError                                  Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    600                                                   body=body, headers=headers,
--> 601                                                   chunked=chunked)
    602 

/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    345         try:
--> 346self._validate_conn(conn)
    347         except (SocketTimeout, BaseSSLError) as e:

/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py in _validate_conn(self, conn)
    849         if not getattr(conn, 'sock', None):  # AppEngine might not have  `.sock`
--> 850conn.connect()
    851 

/anaconda3/lib/python3.6/site-packages/urllib3/connection.py in connect(self)
    325             server_hostname=hostname,
--> 326             ssl_context=context)
    327 

/anaconda3/lib/python3.6/site-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)
    328     if HAS_SNI:  # Platform-specific: OpenSSL with enabled SNI
--> 329return context.wrap_socket(sock, server_hostname=server_hostname)
    330 

/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname)
    447             except OpenSSL.SSL.Error as e:
--> 448raise ssl.SSLError('bad handshake: %r' % e)
    449             break

SSLError: ("bad handshake: SysCallError(54, 'ECONNRESET')",)

During handling of the above exception, another exception occurred:

MaxRetryError                             Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    439                     retries=self.max_retries,
--> 440timeout=timeout
    441                 )

/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    638             retries = retries.increment(method, url, error=e, _pool=self,
--> 639                                         _stacktrace=sys.exc_info()[2])
    640             retries.sleep()

/anaconda3/lib/python3.6/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    387         if new_retry.is_exhausted():
--> 388raise MaxRetryError(_pool, url, error or ResponseError(cause))
    389 

MaxRetryError: HTTPSConnectionPool(host='hosted.datascopeapi.reuters.com', port=443): Max retries exceeded with url: /RestApi/v1/Authentication/RequestToken (Caused by SSLError(SSLError("bad handshake: SysCallError(54, 'ECONNRESET')",),))

During handling of the above exception, another exception occurred:

SSLError                                  Traceback (most recent call last)
<ipython-input-4-ae8e97f611da> in <module>()
      6 header1 = {'Content-Type': 'application/json'}
      7 tokenRequestBody = json.dumps({'Credentials': {'Password': 'xxx', 'Username': 'xxx'}})
----> 8response = requests.post(urlGetToken, tokenRequestBody, headers = header1)
      9 statusCode = response.status_code
     10 if statusCode != 200:

/anaconda3/lib/python3.6/site-packages/requests/api.py in post(url, data, json, **kwargs)
    110     """
    111 
--> 112return request('post', url, data=data, json=json, **kwargs)
    113 
    114 

/anaconda3/lib/python3.6/site-packages/requests/api.py in request(method, url, **kwargs)
     56     # cases, and look like a memory leak in others.
     57     with sessions.Session() as session:
---> 58return session.request(method=method, url=url, **kwargs)
     59 
     60 

/anaconda3/lib/python3.6/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)
    506         }
    507         send_kwargs.update(settings)
--> 508resp = self.send(prep, **send_kwargs)
    509 
    510         return resp

/anaconda3/lib/python3.6/site-packages/requests/sessions.py in send(self, request, **kwargs)
    616 
    617         # Send the request
--> 618r = adapter.send(request, **kwargs)
    619 
    620         # Total elapsed time of the request (approximately)

/anaconda3/lib/python3.6/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    504             if isinstance(e.reason, _SSLError):
    505                 # This branch is for urllib3 v1.22 and later.
--> 506raise SSLError(e, request=request)
    507 
    508             raise ConnectionError(e, request=request)

SSLError: HTTPSConnectionPool(host='hosted.datascopeapi.reuters.com', port=443): Max retries exceeded with url: /RestApi/v1/Authentication/RequestToken (Caused by SSLError(SSLError("bad handshake: SysCallError(54, 'ECONNRESET')",),))
dss-rest-apidatascope-selectdss
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

I can run the code properly. It can connect to the server and get the token.

I don't know how to enable SSL tracing in python. It may relate to your network settings or proxy.

Can you test it with Java Code Examples? You can download the java examples from here.

I tried Java code sample and it failed again. I think you might be right about guessing the root cause should be network settings or proxy. But i could not find the root cause. I am using Mac Book Pro.

What is an error from Java? You can use the following JVM argument to enable tracing.

-Djavax.net.debug=all:handshake:verbose

@jian.kang, did you find the root cause of this issue, did you manage to solve it ?

1 Answer

Upvotes
Accepted
13.7k 26 8 12

@jian.kang

Your code is functional, and works fine for me with the same Python version (3.6) on Windows.

This looks like a proxy or firewall issue, especially if you changed nothing to that part of your code, which worked initially but fails since a few days ago. You might want to discuss with your IT team if they changed anything recently.

Can you try running your code from a different machine on a different network, maybe even from home ? That would prove this assumption.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Yes, Christiaan. Your assumption is correct. I have consulted with DSS REST API QA team here and some of their team members have similar issues with mine. The issue is coming from DNS resolving and proxy servers. I can run my code now and thank you very much for your idea.

Click below to post an Idea Post Idea