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 27 ■ PROJECT 8: FILE SHARING WITH XML-RPC 501<br />

Useful Tools<br />

In this project, you’ll use quite a few standard library modules. I won’t describe them all in<br />

detail; refer <strong>to</strong> Chapter 10 and the <strong>Python</strong> Library Reference for additional details.<br />

The main modules you’ll be using are xmlrpclib and its close friend SimpleXMLRPCServer.<br />

The xmlrpclib module is discussed in Chapter 15, and SimpleXMLRPCServer is explored in more<br />

detail in this chapter.<br />

■Caution Make sure you have an updated version of xmlrpclib and SimpleXMLRPCServer <strong>to</strong> avoid<br />

some security problems present in older versions. See the caution in the “XML-RPC” section of Chapter 15.<br />

For the interface <strong>to</strong> the file sharing program, you’ll be using a module from the standard<br />

library, called cmd (already mentioned in Chapter 24); <strong>to</strong> get some (very limited) parallelism,<br />

you’ll use the threading module, and <strong>to</strong> extract the components of a URL, you’ll use the<br />

urlparse module. All of these modules are explained later in the chapter.<br />

Other modules you might want <strong>to</strong> brush up on are random, string, time, and os.path.<br />

Preparations<br />

First of all, you must make sure you have the proper libraries (that you can import xmlrpclib<br />

and SimpleXMLRPCServer). Make sure you’ve got an updated version of SimpleXMLRPCServer.<br />

You don’t strictly have <strong>to</strong> be connected <strong>to</strong> a network <strong>to</strong> use the software in this project, but<br />

it will make things more interesting. If you have access <strong>to</strong> two (or more) separate machines that<br />

are connected, you can run the software on each of these machines and have them communicate<br />

with each other. For testing purposes, it is also possible <strong>to</strong> run multiple file sharing nodes<br />

on the same machine.<br />

First Implementation<br />

Before you can write a first pro<strong>to</strong>type of the Node class (a single node or peer in the system), you<br />

have <strong>to</strong> learn a bit about how the SimpleXMLRPCServer class works. It is instantiated with a tuple<br />

of the form (servername, port). The server name is the name of the machine on which the<br />

server will run (you can use an empty string here <strong>to</strong> indicate localhost, the machine where<br />

you’re actually executing the program). The port number can be any port you have access <strong>to</strong>,<br />

typically 1024 and above.<br />

After you have instantiated the server, you may register an instance that implements its<br />

“remote methods,” with the register_instance method. Alternatively, you can register individual<br />

functions with the register_function method. When you’re ready <strong>to</strong> run the server (so<br />

that it can respond <strong>to</strong> requests from outside), you call its method serve_forever. You can easily<br />

try this out. Start two interactive <strong>Python</strong> interpreters. In the first one, enter the code that appears<br />

after the caution.

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

Saved successfully!

Ooh no, something went wrong!