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