/Demo/rpc/mountclient.py

http://unladen-swallow.googlecode.com/ · Python · 202 lines · 93 code · 42 blank · 67 comment · 16 complexity · af2703ac174bbab926353e896c82e1fd MD5 · raw file

  1. # Mount RPC client -- RFC 1094 (NFS), Appendix A
  2. # This module demonstrates how to write your own RPC client in Python.
  3. # When this example was written, there was no RPC compiler for
  4. # Python. Without such a compiler, you must first create classes
  5. # derived from Packer and Unpacker to handle the data types for the
  6. # server you want to interface to. You then write the client class.
  7. # If you want to support both the TCP and the UDP version of a
  8. # protocol, use multiple inheritance as shown below.
  9. import rpc
  10. from rpc import Packer, Unpacker, TCPClient, UDPClient
  11. # Program number and version for the mount protocol
  12. MOUNTPROG = 100005
  13. MOUNTVERS = 1
  14. # Size of the 'fhandle' opaque structure
  15. FHSIZE = 32
  16. # Packer derived class for Mount protocol clients.
  17. # The only thing we need to pack beyond basic types is an 'fhandle'
  18. class MountPacker(Packer):
  19. def pack_fhandle(self, fhandle):
  20. self.pack_fopaque(FHSIZE, fhandle)
  21. # Unpacker derived class for Mount protocol clients.
  22. # The important types we need to unpack are fhandle, fhstatus,
  23. # mountlist and exportlist; mountstruct, exportstruct and groups are
  24. # used to unpack components of mountlist and exportlist and the
  25. # corresponding functions are passed as function argument to the
  26. # generic unpack_list function.
  27. class MountUnpacker(Unpacker):
  28. def unpack_fhandle(self):
  29. return self.unpack_fopaque(FHSIZE)
  30. def unpack_fhstatus(self):
  31. status = self.unpack_uint()
  32. if status == 0:
  33. fh = self.unpack_fhandle()
  34. else:
  35. fh = None
  36. return status, fh
  37. def unpack_mountlist(self):
  38. return self.unpack_list(self.unpack_mountstruct)
  39. def unpack_mountstruct(self):
  40. hostname = self.unpack_string()
  41. directory = self.unpack_string()
  42. return (hostname, directory)
  43. def unpack_exportlist(self):
  44. return self.unpack_list(self.unpack_exportstruct)
  45. def unpack_exportstruct(self):
  46. filesys = self.unpack_string()
  47. groups = self.unpack_groups()
  48. return (filesys, groups)
  49. def unpack_groups(self):
  50. return self.unpack_list(self.unpack_string)
  51. # These are the procedures specific to the Mount client class.
  52. # Think of this as a derived class of either TCPClient or UDPClient.
  53. class PartialMountClient:
  54. # This method is called by Client.__init__ to initialize
  55. # self.packer and self.unpacker
  56. def addpackers(self):
  57. self.packer = MountPacker()
  58. self.unpacker = MountUnpacker('')
  59. # This method is called by Client.__init__ to bind the socket
  60. # to a particular network interface and port. We use the
  61. # default network interface, but if we're running as root,
  62. # we want to bind to a reserved port
  63. def bindsocket(self):
  64. import os
  65. try:
  66. uid = os.getuid()
  67. except AttributeError:
  68. uid = 1
  69. if uid == 0:
  70. port = rpc.bindresvport(self.sock, '')
  71. # 'port' is not used
  72. else:
  73. self.sock.bind(('', 0))
  74. # This function is called to cough up a suitable
  75. # authentication object for a call to procedure 'proc'.
  76. def mkcred(self):
  77. if self.cred is None:
  78. self.cred = rpc.AUTH_UNIX, rpc.make_auth_unix_default()
  79. return self.cred
  80. # The methods Mnt, Dump etc. each implement one Remote
  81. # Procedure Call. This is done by calling self.make_call()
  82. # with as arguments:
  83. #
  84. # - the procedure number
  85. # - the arguments (or None)
  86. # - the "packer" function for the arguments (or None)
  87. # - the "unpacker" function for the return value (or None)
  88. #
  89. # The packer and unpacker function, if not None, *must* be
  90. # methods of self.packer and self.unpacker, respectively.
  91. # A value of None means that there are no arguments or is no
  92. # return value, respectively.
  93. #
  94. # The return value from make_call() is the return value from
  95. # the remote procedure call, as unpacked by the "unpacker"
  96. # function, or None if the unpacker function is None.
  97. #
  98. # (Even if you expect a result of None, you should still
  99. # return the return value from make_call(), since this may be
  100. # needed by a broadcasting version of the class.)
  101. #
  102. # If the call fails, make_call() raises an exception
  103. # (this includes time-outs and invalid results).
  104. #
  105. # Note that (at least with the UDP protocol) there is no
  106. # guarantee that a call is executed at most once. When you do
  107. # get a reply, you know it has been executed at least once;
  108. # when you don't get a reply, you know nothing.
  109. def Mnt(self, directory):
  110. return self.make_call(1, directory, \
  111. self.packer.pack_string, \
  112. self.unpacker.unpack_fhstatus)
  113. def Dump(self):
  114. return self.make_call(2, None, \
  115. None, self.unpacker.unpack_mountlist)
  116. def Umnt(self, directory):
  117. return self.make_call(3, directory, \
  118. self.packer.pack_string, None)
  119. def Umntall(self):
  120. return self.make_call(4, None, None, None)
  121. def Export(self):
  122. return self.make_call(5, None, \
  123. None, self.unpacker.unpack_exportlist)
  124. # We turn the partial Mount client into a full one for either protocol
  125. # by use of multiple inheritance. (In general, when class C has base
  126. # classes B1...Bn, if x is an instance of class C, methods of x are
  127. # searched first in C, then in B1, then in B2, ..., finally in Bn.)
  128. class TCPMountClient(PartialMountClient, TCPClient):
  129. def __init__(self, host):
  130. TCPClient.__init__(self, host, MOUNTPROG, MOUNTVERS)
  131. class UDPMountClient(PartialMountClient, UDPClient):
  132. def __init__(self, host):
  133. UDPClient.__init__(self, host, MOUNTPROG, MOUNTVERS)
  134. # A little test program for the Mount client. This takes a host as
  135. # command line argument (default the local machine), prints its export
  136. # list, and attempts to mount and unmount each exported files system.
  137. # An optional first argument of -t or -u specifies the protocol to use
  138. # (TCP or UDP), default is UDP.
  139. def test():
  140. import sys
  141. if sys.argv[1:] and sys.argv[1] == '-t':
  142. C = TCPMountClient
  143. del sys.argv[1]
  144. elif sys.argv[1:] and sys.argv[1] == '-u':
  145. C = UDPMountClient
  146. del sys.argv[1]
  147. else:
  148. C = UDPMountClient
  149. if sys.argv[1:]: host = sys.argv[1]
  150. else: host = ''
  151. mcl = C(host)
  152. list = mcl.Export()
  153. for item in list:
  154. print item
  155. try:
  156. mcl.Mnt(item[0])
  157. except:
  158. print 'Sorry'
  159. continue
  160. mcl.Umnt(item[0])