04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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 to Chapter 10 and the Python 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 to avoid<br />

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

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

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

you’ll use the threading module, and to 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 to 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 to be connected to a network to use the software in this project, but<br />

it will make things more interesting. If you have access to 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 to run multiple file sharing nodes<br />

on the same machine.<br />

First Implementation<br />

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

have to 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 to indicate localhost, the machine where<br />

you’re actually executing the program). The port number can be any port you have access to,<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 to run the server (so<br />

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

try this out. Start two interactive Python 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!