/usr/share/pyshared/medusa/rpc_client.py is in python-medusa 1:0.5.4-7build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | # -*- Mode: Python -*-
# Copyright 1999, 2000 by eGroups, Inc.
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of
# eGroups not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# EGROUPS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL EGROUPS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import marshal
import socket
import string
import exceptions
import string
import sys
#
# there are three clients in here.
#
# 1) rpc client
# 2) fastrpc client
# 3) async fastrpc client
#
# we hope that *whichever* choice you make, that you will enjoy the
# excellent hand-made construction, and return to do business with us
# again in the near future.
#
class RPC_Error (exceptions.StandardError):
pass
# ===========================================================================
# RPC Client
# ===========================================================================
# request types:
# 0 call
# 1 getattr
# 2 setattr
# 3 repr
# 4 del
class rpc_proxy:
DEBUG = 0
def __init__ (self, conn, oid):
# route around __setattr__
self.__dict__['conn'] = conn
self.__dict__['oid'] = oid
# Warning: be VERY CAREFUL with attribute references, keep
# this __getattr__ in mind!
def __getattr__ (self, attr):
# __getattr__ and __call__
if attr == '__call__':
# 0 == __call__
return self.__remote_call__
elif attr == '__repr__':
# 3 == __repr__
return self.__remote_repr__
elif attr == '__getitem__':
return self.__remote_getitem__
elif attr == '__setitem__':
return self.__remote_setitem__
elif attr == '__len__':
return self.__remote_len__
else:
# 1 == __getattr__
return self.__send_request__ (1, attr)
def __setattr__ (self, attr, value):
return self.__send_request__ (2, (attr, value))
def __del__ (self):
try:
self.__send_request__ (4, None)
except:
import who_calls
info = who_calls.compact_traceback()
print info
def __remote_repr__ (self):
r = self.__send_request__ (3, None)
return '<remote object [%s]>' % r[1:-1]
def __remote_call__ (self, *args):
return self.__send_request__ (0, args)
def __remote_getitem__ (self, key):
return self.__send_request__ (5, key)
def __remote_setitem__ (self, key, value):
return self.__send_request__ (6, (key, value))
def __remote_len__ (self):
return self.__send_request__ (7, None)
_request_types_ = ['call', 'getattr', 'setattr', 'repr', 'del', 'getitem', 'setitem', 'len']
def __send_request__ (self, *args):
if self.DEBUG:
kind = args[0]
print (
'RPC: ==> %s:%08x:%s:%s' % (
self.conn.address,
self.oid,
self._request_types_[kind],
repr(args[1:])
)
)
packet = marshal.dumps ((self.oid,)+args)
# send request
self.conn.send_packet (packet)
# get response
data = self.conn.receive_packet()
# types of response:
# 0: proxy
# 1: error
# 2: marshal'd data
kind, value = marshal.loads (data)
if kind == 0:
# proxy (value == oid)
if self.DEBUG:
print 'RPC: <== proxy(%08x)' % (value)
return rpc_proxy (self.conn, value)
elif kind == 1:
raise RPC_Error, value
else:
if self.DEBUG:
print 'RPC: <== %s' % (repr(value))
return value
class rpc_connection:
cache = {}
def __init__ (self, address):
self.address = address
self.connect ()
def connect (self):
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
s.connect (self.address)
self.socket = s
def receive_packet (self):
packet_len = string.atoi (self.socket.recv (8), 16)
packet = []
while packet_len:
data = self.socket.recv (8192)
packet.append (data)
packet_len = packet_len - len(data)
return string.join (packet, '')
def send_packet (self, packet):
self.socket.send ('%08x%s' % (len(packet), packet))
def rpc_connect (address = ('localhost', 8746)):
if not rpc_connection.cache.has_key (address):
conn = rpc_connection (address)
# get oid of remote object
data = conn.receive_packet()
(oid,) = marshal.loads (data)
rpc_connection.cache[address] = rpc_proxy (conn, oid)
return rpc_connection.cache[address]
# ===========================================================================
# fastrpc client
# ===========================================================================
class fastrpc_proxy:
def __init__ (self, conn, path=()):
self.conn = conn
self.path = path
def __getattr__ (self, attr):
if attr == '__call__':
return self.__method_caller__
else:
return fastrpc_proxy (self.conn, self.path + (attr,))
def __method_caller__ (self, *args):
# send request
packet = marshal.dumps ((self.path, args))
self.conn.send_packet (packet)
# get response
data = self.conn.receive_packet()
error, result = marshal.loads (data)
if error is None:
return result
else:
raise RPC_Error, error
def __repr__ (self):
return '<remote-method-%s at %x>' % (string.join (self.path, '.'), id (self))
def fastrpc_connect (address = ('localhost', 8748)):
if not rpc_connection.cache.has_key (address):
conn = rpc_connection (address)
rpc_connection.cache[address] = fastrpc_proxy (conn)
return rpc_connection.cache[address]
# ===========================================================================
# async fastrpc client
# ===========================================================================
import asynchat
class async_fastrpc_client (asynchat.async_chat):
STATE_LENGTH = 'length state'
STATE_PACKET = 'packet state'
def __init__ (self, address=('idb', 3001)):
asynchat.async_chat.__init__ (self)
if type(address) is type(''):
family = socket.AF_UNIX
else:
family = socket.AF_INET
self.create_socket (family, socket.SOCK_STREAM)
self.address = address
self.request_fifo = []
self.buffer = []
self.pstate = self.STATE_LENGTH
self.set_terminator (8)
self._connected = 0
self.connect (self.address)
def log (self, *args):
pass
def handle_connect (self):
self._connected = 1
def close (self):
self._connected = 0
self.flush_pending_requests ('lost connection to rpc server')
asynchat.async_chat.close(self)
def flush_pending_requests (self, why):
f = self.request_fifo
while len(f):
callback = f.pop(0)
callback (why, None)
def collect_incoming_data (self, data):
self.buffer.append (data)
def found_terminator (self):
self.buffer, data = [], string.join (self.buffer, '')
if self.pstate is self.STATE_LENGTH:
packet_length = string.atoi (data, 16)
self.set_terminator (packet_length)
self.pstate = self.STATE_PACKET
else:
# modified to fix socket leak in chat server, 2000-01-27, schiller@eGroups.net
#self.set_terminator (8)
#self.pstate = self.STATE_LENGTH
error, result = marshal.loads (data)
callback = self.request_fifo.pop(0)
callback (error, result)
self.close() # for chat server
def call_method (self, method, args, callback):
if not self._connected:
# might be a unix socket...
family, type = self.family_and_type
self.create_socket (family, type)
self.connect (self.address)
# push the request out the socket
path = string.split (method, '.')
packet = marshal.dumps ((path, args))
self.push ('%08x%s' % (len(packet), packet))
self.request_fifo.append(callback)
if __name__ == '__main__':
if '-f' in sys.argv:
connect = fastrpc_connect
else:
connect = rpc_connect
print 'connecting...'
c = connect()
print 'calling <remote>.calc.sum (1,2,3)'
print c.calc.sum (1,2,3)
print 'calling <remote>.calc.nonexistent(), expect an exception!'
print c.calc.nonexistent()
|