blob: 318a9d99bb50cd4c53bd4211078543edfd3835f7 [file] [log] [blame]
Guido van Rossume3cafbe1992-12-14 23:25:04 +00001# Mount RPC client -- RFC 1094 (NFS), Appendix A
2
3# This module demonstrates how to write your own RPC client in Python.
Martin v. Löwis401a76d2001-10-11 19:23:28 +00004# 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.
Guido van Rossume3cafbe1992-12-14 23:25:04 +000010
11
Guido van Rossum20f99601992-12-15 20:53:17 +000012import rpc
Guido van Rossume3cafbe1992-12-14 23:25:04 +000013from rpc import Packer, Unpacker, TCPClient, UDPClient
14
Guido van Rossumfd92ac81992-12-20 14:57:17 +000015
16# Program number and version for the mount protocol
Guido van Rossume3cafbe1992-12-14 23:25:04 +000017MOUNTPROG = 100005
18MOUNTVERS = 1
19
Guido van Rossumfd92ac81992-12-20 14:57:17 +000020# Size of the 'fhandle' opaque structure
Guido van Rossume3cafbe1992-12-14 23:25:04 +000021FHSIZE = 32
22
23
24# Packer derived class for Mount protocol clients.
25# The only thing we need to pack beyond basic types is an 'fhandle'
26
27class MountPacker(Packer):
28
Tim Peterse6ddc8b2004-07-18 05:56:09 +000029 def pack_fhandle(self, fhandle):
30 self.pack_fopaque(FHSIZE, fhandle)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000031
32
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.
39
40class MountUnpacker(Unpacker):
41
Tim Peterse6ddc8b2004-07-18 05:56:09 +000042 def unpack_fhandle(self):
43 return self.unpack_fopaque(FHSIZE)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000044
Tim Peterse6ddc8b2004-07-18 05:56:09 +000045 def unpack_fhstatus(self):
46 status = self.unpack_uint()
47 if status == 0:
48 fh = self.unpack_fhandle()
49 else:
50 fh = None
51 return status, fh
Guido van Rossume3cafbe1992-12-14 23:25:04 +000052
Tim Peterse6ddc8b2004-07-18 05:56:09 +000053 def unpack_mountlist(self):
54 return self.unpack_list(self.unpack_mountstruct)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000055
Tim Peterse6ddc8b2004-07-18 05:56:09 +000056 def unpack_mountstruct(self):
57 hostname = self.unpack_string()
58 directory = self.unpack_string()
59 return (hostname, directory)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000060
Tim Peterse6ddc8b2004-07-18 05:56:09 +000061 def unpack_exportlist(self):
62 return self.unpack_list(self.unpack_exportstruct)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000063
Tim Peterse6ddc8b2004-07-18 05:56:09 +000064 def unpack_exportstruct(self):
65 filesys = self.unpack_string()
66 groups = self.unpack_groups()
67 return (filesys, groups)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000068
Tim Peterse6ddc8b2004-07-18 05:56:09 +000069 def unpack_groups(self):
70 return self.unpack_list(self.unpack_string)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000071
72
73# These are the procedures specific to the Mount client class.
74# Think of this as a derived class of either TCPClient or UDPClient.
75
76class PartialMountClient:
77
Tim Peterse6ddc8b2004-07-18 05:56:09 +000078 # This method is called by Client.__init__ to initialize
79 # self.packer and self.unpacker
80 def addpackers(self):
81 self.packer = MountPacker()
82 self.unpacker = MountUnpacker('')
Guido van Rossume3cafbe1992-12-14 23:25:04 +000083
Tim Peterse6ddc8b2004-07-18 05:56:09 +000084 # 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
88 def bindsocket(self):
89 import os
90 try:
91 uid = os.getuid()
92 except AttributeError:
93 uid = 1
94 if uid == 0:
95 port = rpc.bindresvport(self.sock, '')
96 # 'port' is not used
97 else:
98 self.sock.bind(('', 0))
Guido van Rossumfd92ac81992-12-20 14:57:17 +000099
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000100 # This function is called to cough up a suitable
101 # authentication object for a call to procedure 'proc'.
102 def mkcred(self):
103 if self.cred == None:
104 self.cred = rpc.AUTH_UNIX, rpc.make_auth_unix_default()
105 return self.cred
Guido van Rossum20f99601992-12-15 20:53:17 +0000106
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000107 # The methods Mnt, Dump etc. each implement one Remote
108 # Procedure Call. This is done by calling self.make_call()
109 # with as arguments:
110 #
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)
115 #
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.
120 #
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.
124 #
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.)
128 #
129 # If the call fails, make_call() raises an exception
130 # (this includes time-outs and invalid results).
131 #
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.
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000136
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000137 def Mnt(self, directory):
138 return self.make_call(1, directory, \
139 self.packer.pack_string, \
140 self.unpacker.unpack_fhstatus)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000141
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000142 def Dump(self):
143 return self.make_call(2, None, \
144 None, self.unpacker.unpack_mountlist)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000145
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000146 def Umnt(self, directory):
147 return self.make_call(3, directory, \
148 self.packer.pack_string, None)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000149
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000150 def Umntall(self):
151 return self.make_call(4, None, None, None)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000152
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000153 def Export(self):
154 return self.make_call(5, None, \
155 None, self.unpacker.unpack_exportlist)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000156
157
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.)
162
163class TCPMountClient(PartialMountClient, TCPClient):
164
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000165 def __init__(self, host):
166 TCPClient.__init__(self, host, MOUNTPROG, MOUNTVERS)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000167
168
169class UDPMountClient(PartialMountClient, UDPClient):
170
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000171 def __init__(self, host):
172 UDPClient.__init__(self, host, MOUNTPROG, MOUNTVERS)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000173
174
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
Guido van Rossumfd92ac81992-12-20 14:57:17 +0000177# 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.
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000180
181def test():
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000182 import sys
183 if sys.argv[1:] and sys.argv[1] == '-t':
184 C = TCPMountClient
185 del sys.argv[1]
186 elif sys.argv[1:] and sys.argv[1] == '-u':
187 C = UDPMountClient
188 del sys.argv[1]
189 else:
190 C = UDPMountClient
191 if sys.argv[1:]: host = sys.argv[1]
192 else: host = ''
193 mcl = C(host)
194 list = mcl.Export()
195 for item in list:
196 print item
197 try:
198 mcl.Mnt(item[0])
199 except:
200 print 'Sorry'
201 continue
202 mcl.Umnt(item[0])