CSUA-UPE Python helpsession
help at event – Workshops
Oct 18 2005 - 5:30pm
Oct 18 2005 - 7:00pm
Soda Hall, rm 310
Python is the hottest scripting language now. We could debate the merits of Perl and the many facets of Ruby, but, let's face it. Big companies like Google, ILM, and Pixar use Python in their pipelines. We'll give you a gentle introduction to Python.
Presented by:
- Aaron Steele, software developer and analyst for the Digital MVZ project
- Mike Hoisie, CSUA
- Steven Chan, UPE
Details about Mike Hoisie's file-sharing example
Simpleshare by Michael Hoisie and Barath Raghavan To use this program, copy the files you want to share in the same directory as simpleshare.py. Essentially, this program starts an HTTP server on the local machine, and registers the HTTP addresses of the files in opendht (an online distributed hash table service) through the XML RPC interface (more information at opendht.org). Lets say you run `python simpleshare.py file.txt`. First, it generates the MD5 hash of file.txt. Lets call this H. Next, it stores the tuple (H, 'file.txt http://locahost:8181/file.txt') in opendht. When clients look up the MD5 hash of the file, they will see the filename, as well as the URL of the file. Then, it starts an HTTP server in the local directory which serves the files. #!/usr/bin/python # usage: ./simpleshare.pyimport sys, socket,os, SimpleXMLRPCServer, xmlrpclib, sha, SimpleHTTPServer, SocketServer d=xmlrpclib.ServerProxy('http://opendht.raghavan.org:5851/') u='http://'+socket.gethostname()+':8181/' for f in sys.argv[1:]: k=sha.new(open(f,'rb').read()) print 'Sharing: ', f, ": ", k.hexdigest() d.put(xmlrpclib.Binary(k.digest()), xmlrpclib.Binary(f+' '+(u+f)),9999,"") SocketServer.TCPServer(("",8181), SimpleHTTPServer.SimpleHTTPRequestHandler).serve_forever() Simpleget.py looks in opendht for the data given the MD5 hash. It should return the file name, and the URL of the file. Then, it downloads the file through urllib2, and writes it to disk. #!/usr/bin/python # usage: ./simpleget.py import sys, os, SimpleXMLRPCServer, xmlrpclib, sha, binascii, urllib2 d=xmlrpclib.ServerProxy('http://planetlab13.millennium.berkeley.edu:5851/') k=xmlrpclib.Binary(binascii.unhexlify(sys.argv[1])) v, p = d.get(k,1,xmlrpclib.Binary(""),"") open(v[0].data.split()[0],'wb').write(urllib2.urlopen(v[0].data.split()[1]).read())
Files from the presentation attached.
| Attachment | Size |
|---|---|
| test-soup.py.txt | 2.68 KB |