Pure Python OPC-UA Client and Server, http://freeopcua.github.io/, https://github.com/FreeOpcUa/python-opcua

API is similar to the python bindings of freeopcua c++ client and servers. However there are differences due to a different implementation and to make it more pythonic.

Most code is autogenerated from xml specification using same code as the one that is going to be used for freeopcua C++ client and server. Adding more functionnality shoud be trivial.

with Python3 the server and client do not require any third party libraries. If using python2.7 or pypy you need to install enum34, trollius(asyncio), and futures(concurrent.futures), with pip for example. Server and client can be run with pypy.


Client: what works:
* connection to server, opening channel, session
* browsing and reading attributes value
* gettings nodes by path and nodeids
* creating subscriptions
* subscribing to items for data change
* adding nodes
* tested servers: freeopcua C++, freeopcua Python, prosys
* method call

Client: what is not implemented yet 
* removing nodes 
* subscribing to events
* adding missing modify methods
* certificate handling
* user and password 

in addition testing against more servers should be done


Server: what works:
* creating channel and sessions
* read/set attributes and browse
* gettings nodes by path and nodeids
* autogenerate addres space from spec
* adding nodes to address space
* tested client: freeopcua C++, freeopcua Python, uaexpert, prosys
* datachange subscriptions
* methods

Server: what is not implemented
* events
* security (users, certificates, etc)
* removing nodes 

Example client code:

```
from opcua import ua, Client

class SubHandler(object):
    def data_change(self, handle, node, val, attr):
        print("Python: New data change event", handle, node, val, attr)

    def event(self, handle, event):
        print("Python: New event", handle, event)

if __name__ == "__main__": 
    client = Client("opc.tcp://localhost:4841/freeopcua/server/")
    client.connect()
    
    root = client.get_root_node()

    #getting a variable by path and setting its value attribute
    myvar = root.get_child(["0:Objects", "2:NewObject", "2:MyVariable"])
    var.set_value(ua.Variant([23], ua.VariantType.Int64))
    
    #subscribing to data change event to our variable
    handler = SubHandler()
    sub = client.create_subscription(500, handler)
    sub.subscribe_data_change(myvar)
    
    time.sleep(100)

    client.disconnect()
```

Example server code:

```
    from opcua import ua, Server

    server = Server()
    server.set_endpoint("opc.tcp://localhost:4841/freeopcua/server/")
    server.set_server_name("FreeOpcUa Example Server")
    server.start()
    objects = server.get_objects_node()
    myfolder = objects.add_folder(2, "myfolder")
    myvar = myfolder.add_variable(2, "myvar", 6.7)
    ...
```

# Development

## Running tests:

python tests.py

## Coverage

coverage run tests.py  
coverage html  
firefox htmlcov/index.html  

