1
0
mirror of https://github.com/JoelBender/bacpypes synced 2025-09-28 22:15:23 +08:00
bacpypes/doc/source/tutorial/tutorial001.rst

53 lines
1.5 KiB
ReStructuredText

.. BACpypes tutorial lesson 1
Clients and Servers
===================
While exploring a library like BACpypes, take full advantage of Python being
an interpreted language with an interactive prompt! The code for this tutorial
is also available in the *tutorial* subdirectory of the repository.
This tutorial will be using :class:`comm.Client`, :class:`comm.Server` classes,
and the :func:`comm.bind` function, so start out by importing them::
>>> from bacpypes.comm import Client, Server, bind
This is a long line of text.
Since the server needs to do something when it gets a request, it
needs to provide a function to get it::
>>> class MyServer(Server):
... def indication(self, arg):
... print "working on", arg
... self.response(arg.upper())
...
Now create an instance of this new class and bind the client and server together::
>>> s = MyServer()
>>> bind(c, s)
This only solves the downstream part of the problem, as you can see::
>>> c.request("hi")
working on hi
NotImplementedError: confirmation must be overridden
So now we create a custom client class that does something with the response::
>>> class MyClient(Client):
... def confirmation(self, pdu):
... print "thanks for the", pdu
...
Create an instance of it, bind the client and server together and test it::
>>> c = MyClient()
>>> bind(c, s)
>>> c.request('hi')
working on hi
thanks for the HI
Success!