code:client
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionLast revision | |||
— | code:client [2012/02/08 14:21] – rcunniff | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Writing clients in Python ====== | ||
+ | RTS2 provides three external bindings, which can be used by clients to interact with the RTS2 environment: | ||
+ | |||
+ | - RTS2 protocol | ||
+ | - XML/RPC protocol | ||
+ | - JSON | ||
+ | |||
+ | JSON, standing for [[http:// | ||
+ | |||
+ | <code python> | ||
+ | # import library with RTS2 functions | ||
+ | import login | ||
+ | # for sleep function | ||
+ | import time | ||
+ | # sign on to JSON server | ||
+ | l = login.Login() | ||
+ | # this will pop out dialog box, asking user for credentials | ||
+ | l.signon() | ||
+ | # now set C0 temperature to -20 | ||
+ | l.jsonProxy().setValue(' | ||
+ | temp = float(l.jsonProxy().getValue(' | ||
+ | while abs(float(temp + 20) > 0.5: | ||
+ | print ' | ||
+ | # refresh cache..can be avoided by having timer running update, similar to loop which can be found in rts2ui | ||
+ | l.jsonProxy().executeCommand(' | ||
+ | l.refresh_cache() | ||
+ | temp = float(l.jsonProxy().getValue(' | ||
+ | # wait for 5 seconds | ||
+ | time.sleep(5) | ||
+ | print ' | ||
+ | </ | ||
+ | |||
+ | This code demonstrates how you can write a simple client in Python, which does things similar to C++ client provided in [[http:// | ||
+ | It illustrates the extreme simplicity of the external JSON API (in this case further masked by the Python library). After establishing connection, your client communicates via a kept-alive HTTP connection - so you don't pay the extra price for opening and closing communication. | ||
+ | |||
+ | JSON is much more dense then XML, so messages are shorter, and parameters are passed as URL parameters - you can talk to the server with a web-browser (or wget command), which is handy for debugging. | ||
+ | |||
+ | Errors are propagated as standard Python errors, and as they are not caught, they will ordinarily cause a client to terminate with a traceback writen to standard output. If you wish to catch possible errors, all you have to do is to add try-except blocks. | ||
+ | |||
+ | This also provides basic security, as a login is required. |
code/client.txt · Last modified: 2012/02/08 00:00 (external edit)