Search code examples
javapythonssljythonjson-rpc

SSL illegal state exception in jython


I am getting the following error frequently but not always,

in __call__ return self.__sendself.__name args File "/Lib/cvplibrary/SSLCertificateWrapper.py" line 51 in wrapped res = f*args **kwargs File "/Lib/jsonrpclib/jsonrpc.py" line 240 in _request response = self._run_requestrequest File "/Lib/jsonrpclib/jsonrpc.py" line 254 in _run_request response = self.__transport.request File "/Lib/xmlrpclib.py" line 1264 in request return self.single_requesthost handler request_body verbose File "/Lib/xmlrpclib.py" line 1292 in single_request self.send_contenth request_body File "/Lib/jsonrpclib/jsonrpc.py" line 129 in send_content connection.endheaders File "/Lib/httplib.py" line 997 in endheaders self._send_outputmessage_body File "/Lib/httplib.py" line 850 in _send_output self.sendmsg File "/Lib/httplib.py" line 812 in send self.connect File "/Lib/httplib.py" line 1204 in connect self.sock = ssl.wrap_socketsock self.key_file self.cert_file File "/Lib/_socket.py" line 357 in handle_exception return method_or_function*args **kwargs File "/Lib/_socket.py" line 357 in handle_exception return method_or_function*args **kwargs File "/Lib/ssl.py" line 287 in wrap_socket return SSLSocket File "/Lib/ssl.py" line 116 in __init__ self.do_handshake File "/Lib/ssl.py" line 165 in do_handshake raise SSLErrorSSL_ERROR_SSL e.strerror _socket.SSLError: [Errno 1] Illegal state exception

Following is the code I am trying to execute by using jython,

import jsonrpclib
url = 'https://user:password@localhost/'
ss = jsonrpclib.Server( url )
version = ss.runCmds("ifconfig" )
print version

I am using jsonrpc lib with an added wrapper to the method " _request" as follows

@trust_all_certificates
def _request(self, methodname, params, rpcid=None):
    request = dumps(params, methodname, encoding=self.__encoding,
                    rpcid=rpcid, version=self.__version)
    response = self._run_request(request)
    check_for_errors(response)
    return response['result']

and the wrapper is taken from : http://tech.pedersen-live.com/2010/10/trusting-all-certificates-in-jython/ as follows,

def trust_all_certificates(f):
'''Decorator function that will make it so the context of the    decorated method
will run with our TrustManager that accepts all certificates'''
def wrapped(*args, **kwargs):
    # Only do this if running under Jython
    if 'java' in sys.platform:
        from javax.net.ssl import SSLContext
        SSLContext.setDefault(TRUST_ALL_CONTEXT)
        try:
            res = f(*args, **kwargs)
            return res
        finally:
            SSLContext.setDefault(DEFAULT_CONTEXT)
    else:
        return f(*args, **kwargs)
return wrapped

how can this problem be solved?


Solution

  • I fixed the issue, by increasing the waiting time in ssl.py line number 165 (Jython 3.7 library )

        time.sleep(0.005)  # Necessary apparently for the handler to get into a good state
        try:
            self._sock._handle_channel_future(handshake, "SSL handshake")
        except socket_error, e:
            raise SSLError(SSL_ERROR_SSL, e.strerror)