Socket自带,不用暗黄
python的client
import socket
HOST = "127.0.0.1" # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"Hello, world")
data = s.recv(1024)
print(f"Received {data!r}")
python的server
import socket
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
输出结果:
2、python的opc(kepserver)通讯
安装pip install opcua
client.py(客户端)
import sys
sys.path.insert(0, "..")
from opcua import ua,Client
if __name__ == "__main__":
client = Client("opc.tcp://localhost:4840/freeopcua/server/")
# client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
try:
client.connect()
# sensor1 = objects.add_object('ns=2;s="sens1"', "Sensor1")
# sens1_current = sensor1.add_variable('ns=2;s="sens1_current"', 'Sensor 1 current', 10)
# sens1_current.set_writable()
var = client.get_node('ns=2; s="sens1"')
print(var)
var = client.get_node('ns=2; s="sens1_current"')
print(var.get_value())
dv = ua.DataValue(ua.Variant(200, ua.VariantType.UInt16))
var.set_value(dv)
print(var.get_value())
# 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)
#var.get_data_value() # get value of node as a DataValue object
#var.get_value() # get value of node as a python builtin
#var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
#var.set_value(3.9) # set node value using implicit data type
# myvar = myobj.add_variable(idx, "MyVariable", 6.7)
# myvar.set_writable() # Set MyVariable to be writable by clients
# sensor1 = objects.add_object('ns=2;s="sens1"', "Sensor 1")
# sens1_current = sensor1.add_variable('ns=2;s="sens1_current"', 'Sensor 1 current', 10)
# sens1_current.set_writable()
# sens1_current = root.get_child('ns=2;s="sens1"."Sensor 1"')
# print("sen",sens1_current.get_value())
# Now getting a variable node using its browse path
# myvar1 = root.get_child(["0:Objects","0:Sensor1", "0:sens1_current"])
# print(myvar1.get_value())
# myvar1 = root.get_child(["0:Objects", "0:Sensor1", "0:sens1_current"])
# print(myvar1.get_value())
myvar = root.get_child(["0:Objects", "2:MyObject1", "2:MyVariable"])
print(myvar.get_value())
# myvar = root.get_child(["Objects", "MyObject1", "MyVariable"])
obj = root.get_child(["0:Objects", "2:MyObject1"])
dv = ua.DataValue(ua.Variant(200, ua.VariantType.UInt16))
myvar.set_value(dv)
print(myvar.get_value())
print("myvar is: ", myvar)
print("myvarval is: ", myvar.get_value())
print("myobj is: ", obj)
# Stacked myvar access
# print("myvar is: ", root.get_children()[0].get_children()[1].get_variables()[0].get_value())
finally:
client.disconnect()
service.py 服务端
import sys
sys.path.insert(0, "..")
import time
from opcua import ua, Server
if __name__ == "__main__":
# setup our server
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
# setup our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
print(idx)
# get Objects node, this is where we should put our nodes
objects = server.get_objects_node()
# populating our address space
sensor1 = objects.add_object('ns=2;s="sens1"', "Sensor1")
sens1_current = sensor1.add_variable('ns=2;s="sens1_current"', 'Sensor 1 current', 10)
sens1_current.set_writable()
myobj = objects.add_object(idx, "MyObject1")
myvar = myobj.add_variable(idx, "MyVariable", 123)
myvar.set_writable() # Set MyVariable to be writable by clients
# starting!
server.start()
try:
count = 0
while True:
time.sleep(1)
count += 0.1
myvar.set_value(count)
finally:
#close connection, remove subcsriptions, etc
server.stop()