04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

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

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

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

■Tip You can find an RSS feed of Python news at http://python.org/channews.rdf. This feed is in<br />

RSS 1.0 format, though.<br />

Listing 15-11. Getting the Wired News<br />

from BeautifulSoup import BeautifulStoneSoup<br />

from urllib import urlopen<br />

from textwrap import wrap<br />

URL = 'http://www.wired.com/news/feeds/rss2'<br />

soup = BeautifulStoneSoup(urlopen(URL).read())<br />

for item in soup('item'):<br />

print item.title.string<br />

print '-'*len(item.title.string)<br />

print '\n'.join(wrap(item.description.string))<br />

print '[%s]\n' % item.link.string<br />

XML-RPC<br />

RPC stands for Remote Procedure Call, and XML is a language you’ve already encountered a<br />

few times by now. The XML-RPC Web site at http://www.xmlrpc.com is a good source of information.<br />

The standard library module xmlrpclib module is used to connect to XML-RPC servers,<br />

and SimpleXMLRPCServer is a class you can use to write such servers. In Chapter 27 you will learn<br />

more about the server stuff—here I will only look at the client side.<br />

■Caution The version of SimpleXMLRPCServer that ships with Python 2.2 contains a serious flaw in its<br />

handling of exceptions. If you use this version, the programs in this chapter and Chapters 27 and 28 may<br />

appear to work, but in fact they won’t be working correctly. Either use a newer version of Python (for example,<br />

version 2.3) or download a new version of SimpleXMLRPCServer.py from http://www.sweetapp.com/<br />

xmlrpc, and replace your current version with the downloaded one. Also, in Python 2.2, 2.3, and 2.4 the<br />

XML-RPC code contains some vulnerabilities; see http://www.python.org/security/PSF-2005-001<br />

for information on this, as well as fixes for the problem. The easiest solution is to use either version 2.3.5 or<br />

newer in the 2.3 series, or 2.4.1 or newer in the 2.4 series, as the problems have been fixed there.<br />

To show you how easy xmlrpclib is to use, here is an example that connects to the XML-<br />

RPC server at The Covers Project (http://coversproject.com), a database of cover songs:

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

Saved successfully!

Ooh no, something went wrong!