Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 1 | # 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öwis | 401a76d | 2001-10-11 19:23:28 +0000 | [diff] [blame] | 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. |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 10 | |
| 11 | |
Guido van Rossum | 20f9960 | 1992-12-15 20:53:17 +0000 | [diff] [blame] | 12 | import rpc |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 13 | from rpc import Packer, Unpacker, TCPClient, UDPClient |
| 14 | |
Guido van Rossum | fd92ac8 | 1992-12-20 14:57:17 +0000 | [diff] [blame] | 15 | |
| 16 | # Program number and version for the mount protocol |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 17 | MOUNTPROG = 100005 |
| 18 | MOUNTVERS = 1 |
| 19 | |
Guido van Rossum | fd92ac8 | 1992-12-20 14:57:17 +0000 | [diff] [blame] | 20 | # Size of the 'fhandle' opaque structure |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 21 | FHSIZE = 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 | |
| 27 | class MountPacker(Packer): |
| 28 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 29 | def pack_fhandle(self, fhandle): |
| 30 | self.pack_fopaque(FHSIZE, fhandle) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 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 | |
| 40 | class MountUnpacker(Unpacker): |
| 41 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 42 | def unpack_fhandle(self): |
| 43 | return self.unpack_fopaque(FHSIZE) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 44 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 52 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 53 | def unpack_mountlist(self): |
| 54 | return self.unpack_list(self.unpack_mountstruct) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 55 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 56 | def unpack_mountstruct(self): |
| 57 | hostname = self.unpack_string() |
| 58 | directory = self.unpack_string() |
| 59 | return (hostname, directory) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 60 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 61 | def unpack_exportlist(self): |
| 62 | return self.unpack_list(self.unpack_exportstruct) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 63 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 64 | def unpack_exportstruct(self): |
| 65 | filesys = self.unpack_string() |
| 66 | groups = self.unpack_groups() |
| 67 | return (filesys, groups) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 68 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 69 | def unpack_groups(self): |
| 70 | return self.unpack_list(self.unpack_string) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 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 | |
| 76 | class PartialMountClient: |
| 77 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 78 | # 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 Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 83 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 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 |
| 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 Rossum | fd92ac8 | 1992-12-20 14:57:17 +0000 | [diff] [blame] | 99 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 100 | # This function is called to cough up a suitable |
| 101 | # authentication object for a call to procedure 'proc'. |
| 102 | def mkcred(self): |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 103 | if self.cred is None: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 104 | self.cred = rpc.AUTH_UNIX, rpc.make_auth_unix_default() |
| 105 | return self.cred |
Guido van Rossum | 20f9960 | 1992-12-15 20:53:17 +0000 | [diff] [blame] | 106 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 107 | # 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 Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 136 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 137 | def Mnt(self, directory): |
| 138 | return self.make_call(1, directory, \ |
| 139 | self.packer.pack_string, \ |
| 140 | self.unpacker.unpack_fhstatus) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 141 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 142 | def Dump(self): |
| 143 | return self.make_call(2, None, \ |
| 144 | None, self.unpacker.unpack_mountlist) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 145 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 146 | def Umnt(self, directory): |
| 147 | return self.make_call(3, directory, \ |
| 148 | self.packer.pack_string, None) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 149 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 150 | def Umntall(self): |
| 151 | return self.make_call(4, None, None, None) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 152 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 153 | def Export(self): |
| 154 | return self.make_call(5, None, \ |
| 155 | None, self.unpacker.unpack_exportlist) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 156 | |
| 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 | |
| 163 | class TCPMountClient(PartialMountClient, TCPClient): |
| 164 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 165 | def __init__(self, host): |
| 166 | TCPClient.__init__(self, host, MOUNTPROG, MOUNTVERS) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | class UDPMountClient(PartialMountClient, UDPClient): |
| 170 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 171 | def __init__(self, host): |
| 172 | UDPClient.__init__(self, host, MOUNTPROG, MOUNTVERS) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 173 | |
| 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 Rossum | fd92ac8 | 1992-12-20 14:57:17 +0000 | [diff] [blame] | 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. |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 180 | |
| 181 | def test(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 182 | 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: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 196 | print(item) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 197 | try: |
| 198 | mcl.Mnt(item[0]) |
| 199 | except: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 200 | print('Sorry') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 201 | continue |
| 202 | mcl.Umnt(item[0]) |