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 515<br />

def __init__(self, url, dirname, urlfile):<br />

"""<br />

Sets the url, dirname, and urlfile, and starts the Node<br />

Server in a separate thread.<br />

"""<br />

Cmd.__init__(self)<br />

self.secret = randomString(SECRET_LENGTH)<br />

n = Node(url, dirname, self.secret)<br />

t = Thread(target=n._start)<br />

t.setDaemon(1)<br />

t.start()<br />

# Give the server a head start:<br />

sleep(HEAD_START)<br />

self.server = ServerProxy(url)<br />

for line in open(urlfile):<br />

line = line.strip()<br />

self.server.hello(line)<br />

def do_fetch(self, arg):<br />

"Call the fetch method of the Server."<br />

try:<br />

self.server.fetch(arg, self.secret)<br />

except Fault, f:<br />

if f.faultCode != UNHANDLED: raise<br />

print "Couldn't find the file", arg<br />

def do_exit(self, arg):<br />

"Exit the program."<br />

print<br />

sys.exit()<br />

do_EOF = do_exit # End-Of-File is synonymous with 'exit'<br />

def main():<br />

urlfile, direc<strong>to</strong>ry, url = sys.argv[1:]<br />

client = Client(url, direc<strong>to</strong>ry, urlfile)<br />

client.cmdloop()<br />

if __name__ == '__main__': main()

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

Saved successfully!

Ooh no, something went wrong!