Skip to content

Commit aef276c

Browse files
Hendrik Hesseoroulet
authored andcommitted
username and password extracted from server_url to seperat attributes
setters added
1 parent 6c2020d commit aef276c

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

opcua/client/client.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def __init__(self, url, timeout=4):
9494
"""
9595
self.logger = logging.getLogger(__name__)
9696
self.server_url = urlparse(url)
97+
#take initial username and password from the url
98+
self._username = self.server_url.username
99+
self._password = self.server_url.password
97100
self.name = "Pure Python Client"
98101
self.description = self.name
99102
self.application_uri = "urn:freeopcua:client"
@@ -130,6 +133,20 @@ def find_endpoint(endpoints, security_mode, policy_uri):
130133
raise ua.UaError("No matching endpoints: {0}, {1}".format(
131134
security_mode, policy_uri))
132135

136+
def set_user(self, username):
137+
"""
138+
Set user name for the connection.
139+
initial user from the URL will be overwritten
140+
"""
141+
self._username = username
142+
143+
def set_password(self, pwd):
144+
"""
145+
Set user password for the connection.
146+
initial password from the URL will be overwritten
147+
"""
148+
self._password = pwd
149+
133150
def set_security_string(self, string):
134151
"""
135152
Set SecureConnection mode. String format:
@@ -226,7 +243,7 @@ def connect(self):
226243
self.send_hello()
227244
self.open_secure_channel()
228245
self.create_session()
229-
self.activate_session(username=self.server_url.username, password=self.server_url.password, certificate=self.user_certificate)
246+
self.activate_session(username=self._username, password=self._password, certificate=self.user_certificate)
230247

231248
def disconnect(self):
232249
"""
@@ -423,13 +440,13 @@ def _add_user_auth(self, params, username, password):
423440
# see specs part 4, 7.36.3: if the token is NOT encrypted,
424441
# then the password only contains UTF-8 encoded password
425442
# and EncryptionAlgorithm is null
426-
if self.server_url.password:
443+
if self._password:
427444
self.logger.warning("Sending plain-text password")
428445
params.UserIdentityToken.Password = password
429446
params.UserIdentityToken.EncryptionAlgorithm = ''
430-
elif self.server_url.password:
447+
elif self._password:
431448
data, uri = self._encrypt_password(password, policy_uri)
432-
params.UserIdentityToken.Password = data
449+
params.UserIdentityToken.Password = data
433450
params.UserIdentityToken.EncryptionAlgorithm = uri
434451
params.UserIdentityToken.PolicyId = self.server_policy_id(ua.UserTokenType.UserName, b"username_basic256")
435452

@@ -473,16 +490,16 @@ def create_subscription(self, period, handler):
473490
returns a Subscription object which allow
474491
to subscribe to events or data on server
475492
handler argument is a class with data_change and/or event methods.
476-
period argument is either a publishing interval in seconds or a
493+
period argument is either a publishing interval in seconds or a
477494
CreateSubscriptionParameters instance. The second option should be used,
478495
if the opcua-server has problems with the default options.
479496
These methods will be called when notfication from server are received.
480497
See example-client.py.
481498
Do not do expensive/slow or network operation from these methods
482499
since they are called directly from receiving thread. This is a design choice,
483-
start another thread if you need to do such a thing.
500+
start another thread if you need to do such a thing.
484501
"""
485-
502+
486503
if isinstance(period, ua.CreateSubscriptionParameters):
487504
return Subscription(self.uaclient, period, handler)
488505
params = ua.CreateSubscriptionParameters()

0 commit comments

Comments
 (0)