zeckblad
04-02-2008, 04:16 PM
I have had good success programing in portal and am interested in trying the gateway. I am new to xml-rpc. Do you have any samples of sending and receiving rpc to bridge and nodes using python xmlrpclib on a separate(from portal) laptop python program?
mgenti
04-02-2008, 05:52 PM
Since verson 2.2 python has included a XML-RPC library to easily talk to XML-RPC servers called xmlrpclib. There are several good examples on the internet and included in python's documentation on how to use the built in library. Unfortunately it does not include an asynchronous way of doing this but blocks until it receives the response back from the server. There is an open source library that you can use that will provide this functionality which can be downloaded at http://sourceforge.net/projects/apy. You will need to download both libraries (apy and AsyncXMLRPC) provided for the example bellow to work. This example connects to the SNAP Gateway running on the same machine and then executes the function "funcName" on the node located at address 01.01.01 and then prints a message when it receives the response.
#Python 2.5 builtin modules
import xmlrpclib, logging, asyncore
log = logging.getLogger('test')
#From http://sourceforge.net/projects/apy
import AsyncXMLRPCTransport
class xmlrpctest(object):
def __init__(self, portNum):
self.srv = xmlrpclib.ServerProxy("http://localhost:" + str(portNum), AsyncXMLRPCTransport.AsyncXMLRPCTransport())
self.srv.connectSerial(2,1).addCallbacks(self.conn ectDone)
def connectDone(self, result):
self.srv.rpc(xmlrpclib.Binary('\x00\x00\x01'),
xmlrpclib.Binary('\x01\x01\x01'),
'funcName',
[])
self.srv.waitOnEvent(xmlrpclib.Binary('\x00\x00\x0 1')).addCallbacks(self.testcallback)
def testcallback(self, er):
log.debug("got callback")
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(msecs)03d %(levelname)-8s %(name)-8s %(message)s', datefmt='%H:%M:%S')
xmlrpctest(8080)
while True:
asyncore.poll(0.01)
Please let us know if this example does not cover enough of how you might use Python to connect to the SNAP Gateway.
vBulletin® v3.7.5, Copyright ©2000-2010, Jelsoft Enterprises Ltd.