Going from webscripting to server-client software.

Duncan Grisby dgrisby at uk.research.att.com
Fri Jul 27 12:32:10 CEST 2001

In article <97ae44ee.0107262210.795d3e0e at posting.google.com>,
 Stephen <shriek at gmx.co.uk> wrote:

>Thank you very much, Duncan.  After looking around for a couple of days (hence
>my late followup. sorry), it does seem like my options are 
>(a) CORBA
>(b) SOAP
>(d) Pyro

You might want to add DOPY, which is along the same lines as Pyro:


I don't know anything about it, though.

>CORBA has always been intimidating. So much work just to do "hello world",
>let along build a full app.

People often say this, but it just isn't true, especially if you use
Python. "Hello world" doesn't make a lot of sense in a distributed
application, but here's the full code for a "fortune cookie" service.

First, declare the interface in IDL:

  // fortune.idl
  module Fortune {
    interface CookieServer {
      string get_cookie();

Now convert it to Python declarations with your chosen ORB's IDL
compiler, for example with omniORBpy:

  $ omniidl -bpython fortune.idl

[ orbit-python doesn't have the IDL compiler step -- it always
  compiles it on the fly. ]

First the client, on the Python command line:

  >>> import CORBA, Fortune
  >>> orb = CORBA.ORB_init()
  >>> o = orb.string_to_object("corbaloc::spud.uk.research.att.com/fortune")
  >>> o = o._narrow(Fortune.CookieServer)
  >>> print o.get_cookie()
  Objects are lost only because people look where they are not rather than
  where they are.

  >>> print o.get_cookie()
  "I'd love to go out with you, but I'm staying home to work on my
  cottage cheese sculpture."

There really is a fortune cookie server running on
spud.uk.research.att.com, so you can run this example.

Now the server:

  #!/usr/bin/env python
  import sys, os
  import CORBA, Fortune, Fortune__POA
  FORTUNE_PATH = "/usr/games/fortune"
  class CookieServer_i (Fortune__POA.CookieServer):
      def get_cookie(self):
	  pipe   = os.popen(FORTUNE_PATH)
	  cookie = pipe.read()
	  if pipe.close():
	      # An error occurred with the pipe
	      cookie = "Oh dear, couldn't get a fortune\n"
	  return cookie
  orb = CORBA.ORB_init(sys.argv)
  poa = orb.resolve_initial_references("RootPOA")
  servant = CookieServer_i()
  print orb.object_to_string(servant._this())

That's it. Note that about half the lines of code are nothing to do
with CORBA -- they're just getting the fortune cookie. When you run
the server it prints a long hex string like:


which can be given to orb.string_to_object (without the line breaks).
That IOR string is also the server running on spud.uk.research.att.com.

A version of the server that can be used with the corbaloc URI is just
as simple, but omniORB specific, so I haven't shown that.



 -- Duncan Grisby  \  Research Engineer  --
  -- AT&T Laboratories Cambridge          --
   -- http://www.uk.research.att.com/~dpg1 --

More information about the Python-list mailing list