16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

338 CHAPTER 15 ■ PYTHON AND THE WEB<br />

>>> from xmlrpclib import ServerProxy<br />

>>> server = ServerProxy('http://coversproject.com/RPC.php')<br />

>>> covers = server.covers.Covered('Monty <strong>Python</strong>')<br />

>>> for cover in covers:<br />

... if cover['song'] == 'Brave Sir Robin':<br />

... print cover['artist']<br />

...<br />

Happy Rhodes<br />

The ServerProxy object looks like a normal object with various methods you can call, but<br />

in fact, whenever you call one of its methods, it sends a request <strong>to</strong> the server, which responds<br />

<strong>to</strong> the requests and returns an answer. So, in a way, you’re calling the method covers.Covered<br />

on the server itself. Network programming could hardly be any easier than this. (For more<br />

information about the XML-RPC methods supplied by The Covers Project, see http://<br />

coversproject.com/about/xmlrpc.html.)<br />

■Note XML-RPC procedure (function/method) names may contain dots, as in the preceding example. The<br />

name covers.Covered does not imply the existence of an object named covers on the server—the dots<br />

are only used <strong>to</strong> structure the names.<br />

Here is a slightly more complicated example, which uses the news service Meerkat <strong>to</strong> find<br />

some articles about <strong>Python</strong>:<br />

>>> from xmlrpclib import ServerProxy<br />

>>> query = {'search': '<strong>Python</strong>', 'num_items': 5}<br />

>>> s = ServerProxy('http://www.oreillynet.com/meerkat/xml-rpc/server.php')<br />

>>> items = s.meerkat.getItems(query)<br />

>>> [i['title'] for i in items]<br />

['MacHack: Meet Dylan the Yoot', 'Hap Debugger', 'Project and proposal for<br />

integrating validation with processing pipelines', 'Spam Check', 'ZCoMIX 1.0<br />

Final Released']<br />

>>> items[0].keys()['link', 'description', 'title']<br />

>>> items[3]['link']<br />

'http://aspn.activestate.com/ASPN/Cookbook/<strong>Python</strong>/Recipe/134945'<br />

As you can see, the method meerkat.getItems is called with a mapping parameter that<br />

contains various arguments (in this case a search query and the number of items <strong>to</strong> be returned)<br />

and returns a list of mappings, each of which has a title, a description, and a link. Actually,<br />

there is a lot more <strong>to</strong> Meerkat than this—if you want <strong>to</strong> experiment, take a look at the Meerkat<br />

Web site (http://www.oreillynet.com/meerkat) or one of the many online tu<strong>to</strong>rials about<br />

the Meerkat XML-RPC API (for example, http://oreillynet.com/pub/a/rss/2000/11/14/<br />

meerkat_xmlrpc.html).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!