Going from webscripting to server-client software.
Duncan Grisby
dgrisby at uk.research.att.com
Fri Jul 27 06:32:10 EDT 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
>(c) XMLRPC
>(d) Pyro
You might want to add DOPY, which is along the same lines as Pyro:
http://www.users.cloud9.net/~proteus/dopy/welcome.html
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()
poa.activate_object(servant)
print orb.object_to_string(servant._this())
poa._get_the_POAManager().activate()
orb.run()
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:
IOR:010000001d00000049444c3a466f7274756e652f436f6f6b69655365727665723
a312e300000000001000000000000005c000000010102000d0000003135382e313234
2e36342e330000f90a07000000666f7274756e6500020000000000000008000000010
0000000545441010000001c0000000100000001000100010000000100010509010100
0100000009010100
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.
Cheers,
Duncan.
--
-- Duncan Grisby \ Research Engineer --
-- AT&T Laboratories Cambridge --
-- http://www.uk.research.att.com/~dpg1 --
More information about the Python-list
mailing list