def wrapper(self): try: client = Client(URL) client.connect() func(self, client) finally: client.disconnect()
class HelloClient: def __init__(self, endpoint): self.client = Client(endpoint) def __enter__(self): self.client.connect() return self.client def __exit__(self, exc_type, exc_val, exc_tb): self.client.disconnect()
async def test_context_manager(server): """Context manager calls connect() and disconnect()""" state = [0] async def increment_state(*args, **kwargs): state[0] += 1 # create client and replace instance methods with dummy methods client = Client('opc.tcp://dummy_address:10000') client.connect = increment_state.__get__(client) client.disconnect = increment_state.__get__(client) assert state[0] == 0 async with client: # test if client connected assert state[0] == 1 # test if client disconnected assert state[0] == 2
import sys sys.path.insert(0, "..") import time import logging from IPython import embed from asyncua import Client from asyncua import ua if __name__ == "__main__": logging.basicConfig(level=logging.WARN) client = Client("opc.tcp://asyncua.demo-this.com:51210/UA/SampleServer") try: client.connect() root = client.get_root_node() objects = client.get_objects_node() struct = client.get_node("ns=2;i=10239") struct_array = client.get_node("ns=2;i=10323") before = struct.read_value() before_array = struct_array.read_value() client.load_type_definitions( ) # scan server for custom structures and import them after = struct.read_value() after_array = struct_array.read_value() embed() finally: client.disconnect()
def FINISH_test_connect_basic256(self): c = Client(URL) c.set_security_string("basic256,sign,XXXX") c.connect() c.disconnect()
def test_connect_anonymous(self): c = Client(URL) c.connect() c.disconnect()