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()
def test_basic256(self): client = Client("opc.tcp://" + os.environ['TEST_IP'] + ":" + os.environ['TEST_PORT']) client.application_uri = "urn:127.0.0.1:ASNeG:FTestClient" client.set_security_string( "Basic128Rsa15,SignAndEncrypt,FTestClient.der,FTestClient.pem") client.connect() client.disconnect()
def client_connection(index, server, security_policies_uri, printable=False): client = Client(server["address"]) try: best_endpoint = best_endpoint_selection(client, server, security_policies_uri, printable) client = Client(best_endpoint.EndpointUrl) client.application_uri = "urn:freeopcua:client" client.description = "OPCUA-Client-Kafka-Gateway" client = client_auth(client, server) policy = best_endpoint.SecurityPolicyUri.split('#')[1] if policy != "None": security_string = str(policy) + ',' + str(MessageSecurityMode(best_endpoint.SecurityMode).name) + ',client_certificate.pem' + ',client_key.pem' client.set_security_string(security_string) client.connect() return client except Exception as ex: #print(f"\nEXCEPTION in client connection: {ex.__class__, ex.args}") return
from opcua import Client from firebase import Firebase import csv import time # url and security url= 'opc.tcp://127.0.0.1:8080' client = Client(url) client.set_security_string("Basic256Sha256,SignAndEncrypt,certificate-example.der,private-key-example.pem") # add "server" certificate.dem file and private-key.pem file (Beckhoff_OpcUaServer.der,Beckhoff_OpcUaServer.pem) client.application_uri = "urn:example.org:FreeOpcUa:python-opcua" # use the uri of the server. (urn:BeckhoffAutomation:TcOpcUaServer) # client connection client.connect() print('CLIENT CONNECTED SUCCESSFULLY') # firebase initialization firebaseConfig = { # here the firebase configuration should be entered } firebase = Firebase(firebaseConfig) db = firebase.database() with open('historyAccess_client.csv',mode='a', newline='') as historyAccess: fieldnames= ['Temperature','Pressure','Flow','Time'] thewriter = csv.DictWriter(historyAccess,fieldnames=fieldnames) thewriter.writeheader() while True: iTemp = client.get_node('ns=2;i=3') Temperature = iTemp.get_value() print(f'Temperature is: {Temperature}') iprssure = client.get_node('ns=2;i=2') Pressure = iprssure.get_value()
def create_client(uri=sUri): client = Client(uri) client.application_uri = "urn:S2OPC:localhost" return client
import sys sys.path.insert(0, "..") import logging from IPython import embed from opcua import Client if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) client = Client("opc.tcp://localhost:53530/OPCUA/SimulationServer/") client.set_security_string( "Basic256Sha256,Sign,certificate-example.der,private-key-example.pem") try: client.application_uri = "urn:example.org:FreeOpcUa:python-opcua" client.secure_channel_timeout = 10000 client.session_timeout = 10000 client.connect() root = client.get_root_node() objects = client.get_objects_node() print("childs og objects are: ", objects.get_children()) embed() finally: client.disconnect()
from opcua import Client import flask import time import flask url = "opc.tcp://192.168.1.33:48010" #server url client = Client(url) #set security mode and certificate. Use server's certificate instead. Certificate path need to be modified client.set_security_string( "Basic256,SignAndEncrypt,C:/Users/RSONG/Documents/InduSoft Web Studio v8.1 Projects/Project/Config/uaserver/own/studio.der,C:/Users/RSONG/Documents/InduSoft Web Studio v8.1 Projects/Project/Config/uaserver/own/studio.pem" ) #Set client uri to match server's certificate client.application_uri = "urn:DESKTOP-U34UL7P:Studio:OpcUaServer" def connect_to_server(client): connection_times = 0 while True: connection_times += 1 try: if connection_times > 100: raise ValueError('Failed to connect to opcua server') client.connect() break except: time.sleep(1) print(f"reconnecting to server, {connection_times} times") continue
from opcua import Client from firebase import Firebase import csv import time import datetime # url and security url = 'opc.tcp://7.110.226.20:4840' client = Client(url) client.set_security_string( "Basic256Sha256,SignAndEncrypt,Beckhoff_OpcUaServer.der,Beckhoff_OpcUaServer.pem" ) # add "server" certificate.dem file and private-key.pem file (Beckhoff_OpcUaServer.der,Beckhoff_OpcUaServer.pem) client.application_uri = "urn:BeckhoffAutomation:TcOpcUaServer" # use the uri of the server. (urn:BeckhoffAutomation:TcOpcUaServer) # client connection client.connect() print('CLIENT CONNECTED SUCCESSFULLY') # firebase initialization firebaseConfig = { #the firebase database configuration goes here } firebase = Firebase(firebaseConfig) db = firebase.database() with open('historyAccess_client.csv', mode='a', newline='') as historyAccess: fieldnames = ['Temperature', 'Pressure', 'Flow', 'Time'] thewriter = csv.DictWriter(historyAccess, fieldnames=fieldnames) thewriter.writeheader() while True: iTemp = client.get_node('ns=4;s=MAIN.iTemp') Temperature = iTemp.get_value() print(f'Temperature is: {Temperature}')