Recipe15.6.Giving an XML-RPC Server a wxPython GUI


Recipe 15.6. Giving an XML-RPC Server a wxPython GUI

Credit: Peter Arwanitis, Alex Martelli

Problem

You are writing an XML-RPC server and want to add a GUI to it, or you're writing a GUI application that you want to be able to interact as an XML-RPC server too.

Solution

As long as you use Twisted for the network interaction, and wxPython for the GUI, this task is reasonably easy, since these packages can cooperate through the twisted.internet.wxreactor module. You do need to have specific incantations at the start of your program, as follows:

# To use wxPython and Twisted together, do the following, in exact order: import wx from twisted.internet import wxreactor wxreactor.install( ) from twisted.internet import reactor # Then, have whatever other imports as may be necessary to your program from twisted.web import xmlrpc, server class MyFrame(wx.Frame):     ''' Main window for this wx application. '''     def _ _init_ _(self, parent, ID, title, pos=wx.DefaultPosition,                  size=(200, 100), style=wx.DEFAULT_FRAME_STYLE):         wx.Frame._ _init_ _(self, parent, ID, title, pos, size, style)         wx.EVT_CLOSE(self, self.OnCloseWindow)     def OnCloseWindow(self, event):         self.Destroy( )         reactor.stop( ) class MyXMLRPCApp(wx.App, xmlrpc.XMLRPC):     ''' We're a wx Application _AND_ an XML-RPC server too. '''     def OnInit(self):         ''' wx-related startup code: builds the GUI. '''         self.frame = MyFrame(None, -1, 'Hello')         self.frame.Show(True)         self.SetTopWindow(self.frame)         return True     # methods exposed to XML-RPC clients:     def xmlrpc_stop(self):         """ Closes the wx application. """         self.frame.Close( )         return 'Shutdown initiated'     def xmlrpc_title(self, x):         """ Change the wx application's window's caption. """         self.frame.SetTitle(x)         return 'Title set to %r' % x     def xmlrpc_add(self, x, y):         """ Provide some computational services to clients. """         return x + y if _ _name_ _ == '_ _main_ _':     # pass False to emit stdout/stderr to shell, not an additional wx window     app = MyXMLRPCApp(False)     # Make the wx application twisted-aware     reactor.registerWxApp(app)     # Make a XML-RPC Server listening to port 7080     reactor.listenTCP(7080, server.Site(app))     # Start both reactor parts (wx MainLoop and XML-RPC server)     reactor.run( )

Discussion

It is often useful to give an XML-RPC server a GUI, for example, to display the current status to an operator or administrator. Conversely, it is often useful to give a GUI application the ability to accept remote requests from other programs, and making the application an XML-RPC server is an excellent, simple way to accomplish that purpose.

Either way, if you use Twisted for the networking part, you're off to a good start, because Twisted offers specialized reactor implementations to ease cooperation with several GUI toolkits. In particular, this recipe shows how a Twisted-based XML-RPC server can sport a wxPython GUI thanks to the twisted.internet.wxreactor module.

To try this recipe, save the code from the "Solution" as a Python script and start it from a shell. If you run some kind of "personal firewall" that's normally set to impede TCP/IP communication between programs running on your machine, ensure it's set to let such communication happen on TCP port 7080. Then, from any interactive Python interpreter session on the same machine, do:

>>> import xmlrpclib >>> s = xmlrpclib.ServerProxy('http://localhost:7080') >>> s.add(23, 42) 65 >>> s.title('Changed Title') Title set to 'Changed Title'

Observe that the title of the wx application's window has changed. Now, you can close the application, either by whatever GUI means you normally use on your platform (it is a totally cross-platform application, after all), or by calling s.stop( ) from the same Python interpreter interactive session that we just showed. You can also run such a client on any other machine, as long as it has open TCP/IP connectivity on port 7080 with the machine running the server. (In particular, make sure you open port 7080 on any firewall that would normally block that port, whether the firewall is on either of the machines, or on any other network apparatus that may lie between them.)

Both Twisted and wxPython, while already rich and solid frameworks, are still growing and changing, so it may be important to ensure you have the right releases installed properly on your machine. This recipe should run on any platform that is equipped with Python 2.3 or better, wxPython 2.4.2.4 or better, and Twisted 1.3.0 or better. Of course, we don't have access to every platform in the world, nor to all future releases of these tools, so we tested the recipe only under Windows/XP, Mac OS X 10.3.6, and Linux, with Python 2.3 and 2.4, wxPython 2.4.2.4, and some 2.5.x.y releases, and Twisted 1.3.0 specifically.

Since the recipe relies only on published, supported aspects of the various tools, one can hope that the recipe will also work elsewhere, and will work with future releases of the tools. However, if this recipe's approach does not prove satisfactory for your purposes, you may want to try a different approach based on threads, shown at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286201.

See Also

Twisted's home page is http://www.twistedmatrix.com; documentation on Twisted XML-RPC support is at http://www.twistedmatrix.com/documents/current/howto/xmlrpc; wxPython's home page is http://www.wxpython.org.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net