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.

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

■Tip You can find an RSS feed of <strong>Python</strong> 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 BeautifulS<strong>to</strong>neSoup<br />

from urllib import urlopen<br />

from textwrap import wrap<br />

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

soup = BeautifulS<strong>to</strong>neSoup(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 <strong>to</strong> connect <strong>to</strong> XML-RPC servers,<br />

and SimpleXMLRPCServer is a class you can use <strong>to</strong> 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 <strong>Python</strong> 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 <strong>to</strong> work, but in fact they won’t be working correctly. Either use a newer version of <strong>Python</strong> (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 <strong>Python</strong> 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 <strong>to</strong> 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 <strong>to</strong> use, here is an example that connects <strong>to</strong> 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!