blob: 8a4b1b64d835b577d312dac5486b504fa898a0d7 [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
29 def pack_fhandle(self, fhandle):
30 self.pack_fopaque(FHSIZE, fhandle)
31
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
42 def unpack_fhandle(self):
43 return self.unpack_fopaque(FHSIZE)
44
45 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
52
53 def unpack_mountlist(self):
54 return self.unpack_list(self.unpack_mountstruct)
55
56 def unpack_mountstruct(self):
57 hostname = self.unpack_string()
58 directory = self.unpack_string()
59 return (hostname, directory)
60
61 def unpack_exportlist(self):
62 return self.unpack_list(self.unpack_exportstruct)
63
64 def unpack_exportstruct(self):
65 filesys = self.unpack_string()
66 groups = self.unpack_groups()
67 return (filesys, groups)
68
69 def unpack_groups(self):
70 return self.unpack_list(self.unpack_string)
71
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
Guido van Rossum9e80d6f1993-12-17 14:32:26 +000078 # This method is called by Client.__init__ to initialize
Guido van Rossume3cafbe1992-12-14 23:25:04 +000079 # self.packer and self.unpacker
80 def addpackers(self):
Guido van Rossum9e80d6f1993-12-17 14:32:26 +000081 self.packer = MountPacker()
82 self.unpacker = MountUnpacker('')
Guido van Rossume3cafbe1992-12-14 23:25:04 +000083
Guido van Rossum9e80d6f1993-12-17 14:32:26 +000084 # This method is called by Client.__init__ to bind the socket
Guido van Rossumfd92ac81992-12-20 14:57:17 +000085 # 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
Guido van Rossum73065381992-12-21 14:32:28 +000090 try:
91 uid = os.getuid()
92 except AttributeError:
93 uid = 1
94 if uid == 0:
Guido van Rossumfd92ac81992-12-20 14:57:17 +000095 port = rpc.bindresvport(self.sock, '')
96 # 'port' is not used
97 else:
98 self.sock.bind(('', 0))
99
100 # This function is called to cough up a suitable
Guido van Rossum20f99601992-12-15 20:53:17 +0000101 # authentication object for a call to procedure 'proc'.
Guido van Rossumfd92ac81992-12-20 14:57:17 +0000102 def mkcred(self):
Guido van Rossum20f99601992-12-15 20:53:17 +0000103 if self.cred == None:
104 self.cred = rpc.AUTH_UNIX, rpc.make_auth_unix_default()
105 return self.cred
106
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000107 # The methods Mnt, Dump etc. each implement one Remote
Guido van Rossum73065381992-12-21 14:32:28 +0000108 # 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
137 def Mnt(self, directory):
Guido van Rossum73065381992-12-21 14:32:28 +0000138 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
142 def Dump(self):
Guido van Rossum73065381992-12-21 14:32:28 +0000143 return self.make_call(2, None, \
144 None, self.unpacker.unpack_mountlist)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000145
146 def Umnt(self, directory):
Guido van Rossum73065381992-12-21 14:32:28 +0000147 return self.make_call(3, directory, \
148 self.packer.pack_string, None)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000149
150 def Umntall(self):
Guido van Rossum73065381992-12-21 14:32:28 +0000151 return self.make_call(4, None, None, None)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000152
153 def Export(self):
Guido van Rossum73065381992-12-21 14:32:28 +0000154 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
Guido van Rossum9e80d6f1993-12-17 14:32:26 +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
Guido van Rossum9e80d6f1993-12-17 14:32:26 +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():
182 import sys
Guido van Rossumfd92ac81992-12-20 14:57:17 +0000183 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
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000191 if sys.argv[1:]: host = sys.argv[1]
192 else: host = ''
Guido van Rossum9e80d6f1993-12-17 14:32:26 +0000193 mcl = C(host)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000194 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])