FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.36k stars 658 forks source link

concurrent.futures._base.CancelledError upon open_secure_channel attempt #1155

Open acbarber1 opened 3 years ago

acbarber1 commented 3 years ago

Hi all,

I'm trying to connect to an Ignition Server but am running into an error similar to those seen on this thread. I have verified that we are able to connect with UA expert using same username, password, cert and key. Here's my skeleton code:

# Change These
OPC_IP="<ip here>"
OPC_PORT="62541"
CERTIFICATE_FILENAME="/data/notebooks/sm_cert.pem"
PRIVATE_KEY_FILENAME="/data/notebooks/sm_key.pem"
SERVER_CERT_FILENAME="/data/notebooks/ignition_cert.der"
USERNAME = "<username>"
PASSWORD = "<pass>"

# Which Compose Higher Level elements

APPLICATION_URI="<application_uri>"

DISCOVERY_OPTION='/discovery'
OPC_CONNECT_STRING=f'opc.tcp://{OPC_IP}:{OPC_PORT}{DISCOVERY_OPTION}'

# Now Connect

print (f"Connecting... to {OPC_CONNECT_STRING}")
time.sleep(1)
client = Client(OPC_CONNECT_STRING,timeout=6)
client.set_user(USERNAME)
client.set_password(PASSWORD)

client.set_security(policy=security_policies.SecurityPolicyBasic256Sha256,
                    certificate_path=CERTIFICATE_FILENAME,
                    private_key_path=PRIVATE_KEY_FILENAME,
                    server_certificate_path=SERVER_CERT_FILENAME)
client.application_uri = APPLICATION_URI

print("Client Created moving to active connect")
time.sleep(2)

client.connect_socket()
print('sending hello...')
print('opening secure channel...')
client.open_secure_channel(renew=False)
print('creating session...')
client.create_session()

However, the above code results in the following error:

exception calling callback for <Future at 0x7fb51376b128 state=cancelled>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 324, in _invoke_callbacks
    callback(self)
  File "/usr/local/lib/python3.7/site-packages/opcua/client/ua_client.py", line 208, in clb
    response = struct_from_binary(ua.OpenSecureChannelResponse, future.result())
  File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 423, in result
    raise CancelledError()
concurrent.futures._base.CancelledError

sending hello...
opening secure channel...

---------------------------------------------------------------------------
TimeoutError                              Traceback (most recent call last)
<ipython-input-4-414808430c6b> in <module>
     44 client.send_hello()
     45 print('opening secure channel...')
---> 46 client.open_secure_channel(renew=False)
     47 print('creating session...')
     48 client.create_session()

/usr/local/lib/python3.7/site-packages/opcua/client/client.py in open_secure_channel(self, renew)
    334         print("client security_policy: ",self.security_policy)
    335         params.ClientNonce = utils.create_nonce(self.security_policy.symmetric_key_size) # this nonce is used to create a symmetric key
--> 336         result = self.uaclient.open_secure_channel(params)
    337         if self.secure_channel_timeout != result.SecurityToken.RevisedLifetime:
    338             _logger.warning("Requested secure channel timeout to be %dms, got %dms instead",

/usr/local/lib/python3.7/site-packages/opcua/client/ua_client.py in open_secure_channel(self, params)
    281 
    282     def open_secure_channel(self, params):
--> 283         return self._uasocket.open_secure_channel(params)
    284 
    285     def close_secure_channel(self):

/usr/local/lib/python3.7/site-packages/opcua/client/ua_client.py in open_secure_channel(self, params)
    215         # wait for our callbackto finish rexecuting before returning
    216         print(request)
--> 217         response = clb.future.result(self.timeout)
    218         response.ResponseHeader.ServiceResult.check()
    219 

/usr/local/lib/python3.7/concurrent/futures/_base.py in result(self, timeout)
    432                 return self.__get_result()
    433             else:
--> 434                 raise TimeoutError()
    435 
    436     def exception(self, timeout=None):

TimeoutError: 

So it appears the cancelled error is thrown somewhere in the process of opening a secure channel. Here are the params being used by the open_secure_channel function:

ClientProtocolVersion:  0
RequestType:  SecurityTokenRequestType.Issue
SecurityMode:  MessageSecurityMode.SignAndEncrypt
ClientNonce:  b"<client_nonce_here>"
RequestedLifetime:  3600000

I am using the most recent version of the library (0.98.12). Any insight on this error would be greatly appreciated!

cc: @oroulet

Originally posted by @acbarber1 in https://github.com/FreeOpcUa/python-opcua/issues/845#issuecomment-720558577

HARIGB commented 2 years ago

Hi @acbarber1,

Is it resolved, Even i am trying to connect my Ignition server with this python opcua library and seeing same error (concurrent.futures._base.CancelledError) Did you able to find any other alternative solution ?