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