Example #1
1
def conn_opc():
    # OPCサーバに接続
    cl = Client("opc.tcp://192.168.10.5:51110/CogentDataHub/DataAccess")
    # クライアント証明書のapplication_uri
    cl.application_uri = "urn:desktop-i50i89m:Cogent DataHub"
    # policy設定
    print("secPolicy: " + str(secPolicy))
    if secPolicy != policies[0]:  # None以外の場合SecurityPolicyを設定
        mode = ua.MessageSecurityMode.SignAndEncrypt
        pc = getattr(security_policies, 'SecurityPolicy' + secPolicy)
        # 第二引数:クライアント証明書
        cl.set_security(
            pc, "/Users/watarium/PycharmProjects/opcua/OPCUA_CL.der",
            "/Users/watarium/PycharmProjects/opcua/OPCUAClient.pem",
            "/Users/watarium/PycharmProjects/opcua/OPCUAServer.der", mode)

    # 認証設定
    if setCert == certs[1]:  # user/pass
        cl.set_user("admin")
        cl.set_password("1234")
    elif setCert == certs[2]:  # certificate
        cl.load_private_key(
            "/Users/watarium/PycharmProjects/opcua/OPCUAClient.pem")
        cl.load_client_certificate(
            "/Users/watarium/PycharmProjects/opcua/OPCUA_CL.der")
    try:
        # 接続
        print("Policy: {0}, Certificate: {1}".format(secPolicy, setCert))
        print("---------------------Connection start-----------------------")
        cl.connect()
        sleep(5)
        # 情報取得
        ep = cl.get_endpoints()
        print(ep[0].Server.ApplicationUri)

        root = cl.get_root_node()
        print("Objects node is: ", root)

        print(cl.get_namespace_array())
        print(cl.get_namespace_index('urn:win-9hi38ajrojd:Cogent DataHub'))

        #これがうまくいかなかった(2019/06/27)
        #node = cl.get_node('ns=1;s=xxxxx')
        #print(node.get_value())

        #node.set_attribute(ua.AttributeIds.Value, 1)

        # 切断
        cl.disconnect()
        print("-------------------Connection Success!!!--------------------")

    except Exception as e:
        print("---------------------Connection Faild!!---------------------")
        print(e)
        cl.disconnect()
Example #2
0
def uaclient():
    parser = argparse.ArgumentParser(
        description=
        "Connect to server and start python shell. root and objects nodes are available. Node specificed in command line is available as mynode variable"
    )
    add_common_args(parser)
    parser.add_argument("-c", "--certificate", help="set client certificate")
    parser.add_argument("-k", "--private_key", help="set client private key")
    args = parse_args(parser)

    client = Client(args.url, timeout=args.timeout)
    client.set_security_string(args.security)
    if args.certificate:
        client.load_client_certificate(args.certificate)
    if args.private_key:
        client.load_private_key(args.private_key)
    client.connect()
    try:
        root = client.get_root_node()
        objects = client.get_objects_node()
        mynode = get_node(client, args)
        embed()
    finally:
        client.disconnect()
    sys.exit(0)
Example #3
0
def uaclient():
    parser = argparse.ArgumentParser(description="Connect to server and start python shell. root and objects nodes are available. Node specificed in command line is available as mynode variable")
    add_common_args(parser)
    parser.add_argument("-c",
                        "--certificate",
                        help="set client certificate")
    parser.add_argument("-k",
                        "--private_key",
                        help="set client private key")
    args = parse_args(parser)

    client = Client(args.url, timeout=args.timeout)
    _configure_client_with_args(client, args)
    if args.certificate:
        client.load_client_certificate(args.certificate)
    if args.private_key:
        client.load_private_key(args.private_key)
    client.connect()
    try:
        root = client.get_root_node()
        objects = client.get_objects_node()
        mynode = get_node(client, args)
        embed()
    finally:
        client.disconnect()
    sys.exit(0)
    client = Client("opc.tcp://localhost:4840")
    uasecurity = UaSecurity()
    if uasecurity.get_securitytype() == 'tls':
        server_cert, client_cert, private_key = uasecurity.get_certificates()
        if server_cert is None:
            print(
                'tls is enabled, but server cert is missing with current configuration'
            )
            sys.exit(-1)
        if private_key is None:
            print(
                'tls is enabled, but private key is missing with current configuration'
            )
            sys.exit(-1)
        client.load_client_certificate(server_cert)
        client.load_private_key(private_key)

    try:
        client.connect()

        # Client has a few methods to get proxy to UA nodes that should always
        # be in address space such as Root or Objects
        root = client.get_root_node()
        print("Root node is: ", root)
        objects = client.get_objects_node()
        #print("Objects node is: ", objects)

        # Node objects have methods to read and write node attributes as well as browse or populate address space
        #print("Children of root are: ", root.get_children())

        #obj = root.get_child(["0:Objects"])
Example #5
0
import sys
sys.path.insert(0, "..")

from opcua import Client, ua
from opcua.crypto import security_policies

if __name__ == "__main__":

    client = Client("opc.tcp://*****:*****@localhost:4840/freeopcua/server/") #connect using a user
    try:
        client.connect()

        # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
        root = client.get_root_node()
        print("Objects node is: ", root)

        # Node objects have methods to read and write node attributes as well as browse or populate address space
        print("Children of root are: ", root.get_children())

        # get a specific node knowing its node id
        #var = client.get_node(ua.NodeId(1002, 2))
        #var = client.get_node("ns=3;i=2002")
        #print(var)
import sys

sys.path.insert(0, "..")
import logging

from IPython import embed

from opcua import Client


if __name__ == "__main__":
    logging.basicConfig(level=logging.WARN)
    client = Client("opc.tcp://localhost:53530/OPCUA/SimulationServer/")
    client.load_client_certificate("server_cert.pem")
    client.load_private_key("mykey.pem")
    try:
        client.connect()
        root = client.get_root_node()
        objects = client.get_objects_node()
        print("childs og objects are: ", objects.get_children())

        embed()
    finally:
        client.disconnect()