Passed
Pull Request — master (#184)
by Olivier
02:17
created

server-with-encryption.main()   A

Complexity

Conditions 3

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 20
nop 0
dl 0
loc 34
rs 9.4
c 0
b 0
f 0
1
import sys
2
import asyncio
3
import logging
4
5
sys.path.insert(0, "..")
6
7
from asyncua import Server, ua
8
9
10
async def main():
11
12
    # setup our server
13
    server = Server()
14
    await server.init()
15
    server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
16
17
    # load server certificate and private key. This enables endpoints
18
    # with signing and encryption.
19
    await server.load_certificate("certificate-example.der")
20
    await server.load_private_key("private-key-example.pem")
21
22
    # set all possible endpoint policies for clients to connect through
23
    server.set_security_policy([
24
        ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt,
25
        ua.SecurityPolicyType.Basic256Sha256_Sign,
26
    ])
27
28
    # setup our own namespace, not really necessary but should as spec
29
    uri = "http://examples.freeopcua.github.io"
30
    idx = await server.register_namespace(uri)
31
32
    # populating our address space
33
    myobj = await server.nodes.objects.add_object(idx, "MyObject")
34
    myvar = await myobj.add_variable(idx, "MyVariable", 6.7)
35
    await myvar.set_writable()  # Set MyVariable to be writable by clients
36
37
    # starting!
38
    async with server:
39
        count = 0
40
        while True:
41
            await asyncio.sleep(1)
42
            count += 0.1
43
            await myvar.write_value(count)
44
45
46
if __name__ == "__main__":
47
48
    logging.basicConfig(level=logging.INFO)
49
    asyncio.run(main())
50