client.py :  » Database » Python-Remote-Objects » Pyro-3.10 » examples » filetransfer » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » Python Remote Objects 
Python Remote Objects » Pyro 3.10 » examples » filetransfer » client.py
#!/usr/bin/env python

#
#  Client for the extremely simple 'file-server' using Pyro.
#
#  NOTE: this is just an example to show how you _could_ send
#  large streams of data with Pyro (by transferring it in chunks).
#  THIS CODE CAN NOT COPE WITH MULTIPLE TRANSFERS AT THE SAME TIME!
#  (because the file server is statefull and only knows about
#  a single file transfer that is in progress). Also, no measures
#  are taken to protect the file transfer from other clients that
#  connect to the server and 'hijack' the download by calling
#  retrieveNextChunk.
#

import Pyro.core
import sys,time

class FileClient(object):
  def __init__(self):
    self.fileserver = Pyro.core.getProxyForURI("PYRONAME://fileserver")
  def menu(self):
    print "\nls : list directory"
    print "r <file> : retrieve file at once"
    print "c <file> : retrieve file in chunks"
    print "q : quit"
  def cli(self):
    while True:
      self.menu()
      cmd=raw_input("?>")
      if cmd=="ls":
        self.ls()
      elif cmd.startswith("r "):
        self.retrieveAtOnce(cmd[2:])
      elif cmd.startswith("c "):
        self.retrieveChunks(cmd[2:])
      elif cmd=="q":
        return
      else:
        print "invalid command"
  def ls(self):
    files = self.fileserver.listdir()
    files.sort()
    #for d in dirs:
    #  print "  %s [DIRECTORY]" % d
    for f in files:
      print f

  def retrieveAtOnce(self,file):
    print "Retrieving",file,"..."
    starttime=time.time()
    try:
      data=self.fileserver.retrieveAtOnce(file)
    except IOError,x:
      print "error: ",x
    else:
      duration=time.time()-starttime
      print len(data),"bytes received in",int(duration),"seconds",
      if duration>0:
        print "=",int(len(data)/duration/1024.0),"kb/sec"
      open(file,"wb").write(data)
      print "saved to",file
      
  def retrieveChunks(self,file):
    print "Retrieving",file,"..."
    starttime=time.time()
    try:
      size = self.fileserver.openFile(file)
    except IOError,x:
      print "error: ",x
    else:
      print "Filesize=",size
      total=0
      file=open(file,"wb")
      while True:
        chunk=self.fileserver.retrieveNextChunk()
        sys.stdout.write(".")
        sys.stdout.flush()
        if chunk:
          file.write(chunk)
          total+=len(chunk)
        else:
          break
      self.fileserver.closeFile()
      file.close()
      duration=time.time()-starttime
      print total,"bytes received in",int(duration),"seconds",
      if duration>0:
        print "=",int(total/duration/1024.0),"kb/sec"
    

def main(args):
  Pyro.core.initClient()
  client=FileClient()
  client.cli()

if __name__=="__main__":
  main(sys.argv)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.