comparison rpc.py @ 56:91b476ebc0f2

Run through 2to3
author Daniel O'Connor <doconnor@gsoft.com.au>
date Tue, 08 Dec 2020 14:00:45 +1030
parents cba1c44060f5
children
comparison
equal deleted inserted replaced
55:ad5942d22f78 56:91b476ebc0f2
99 return (flavor, stuff) 99 return (flavor, stuff)
100 100
101 def unpack_callheader(self): 101 def unpack_callheader(self):
102 xid = self.unpack_uint(xid) 102 xid = self.unpack_uint(xid)
103 temp = self.unpack_enum() 103 temp = self.unpack_enum()
104 if temp <> CALL: 104 if temp != CALL:
105 raise BadRPCFormat, 'no CALL but ' + `temp` 105 raise BadRPCFormat('no CALL but ' + repr(temp))
106 temp = self.unpack_uint() 106 temp = self.unpack_uint()
107 if temp <> RPCVERSION: 107 if temp != RPCVERSION:
108 raise BadRPCVerspion, 'bad RPC version ' + `temp` 108 raise BadRPCVerspion('bad RPC version ' + repr(temp))
109 prog = self.unpack_uint() 109 prog = self.unpack_uint()
110 vers = self.unpack_uint() 110 vers = self.unpack_uint()
111 proc = self.unpack_uint() 111 proc = self.unpack_uint()
112 cred = self.unpack_auth() 112 cred = self.unpack_auth()
113 verf = self.unpack_auth() 113 verf = self.unpack_auth()
115 # Caller must add procedure-specific part of call 115 # Caller must add procedure-specific part of call
116 116
117 def unpack_replyheader(self): 117 def unpack_replyheader(self):
118 xid = self.unpack_uint() 118 xid = self.unpack_uint()
119 mtype = self.unpack_enum() 119 mtype = self.unpack_enum()
120 if mtype <> REPLY: 120 if mtype != REPLY:
121 raise RuntimeError, 'no REPLY but ' + `mtype` 121 raise RuntimeError('no REPLY but ' + repr(mtype))
122 stat = self.unpack_enum() 122 stat = self.unpack_enum()
123 if stat == MSG_DENIED: 123 if stat == MSG_DENIED:
124 stat = self.unpack_enum() 124 stat = self.unpack_enum()
125 if stat == RPC_MISMATCH: 125 if stat == RPC_MISMATCH:
126 low = self.unpack_uint() 126 low = self.unpack_uint()
127 high = self.unpack_uint() 127 high = self.unpack_uint()
128 raise RuntimeError, \ 128 raise RuntimeError('MSG_DENIED: RPC_MISMATCH: ' + repr((low, high)))
129 'MSG_DENIED: RPC_MISMATCH: ' + `low, high`
130 if stat == AUTH_ERROR: 129 if stat == AUTH_ERROR:
131 stat = self.unpack_uint() 130 stat = self.unpack_uint()
132 raise RuntimeError, \ 131 raise RuntimeError('MSG_DENIED: AUTH_ERROR: ' + repr(stat))
133 'MSG_DENIED: AUTH_ERROR: ' + `stat` 132 raise RuntimeError('MSG_DENIED: ' + repr(stat))
134 raise RuntimeError, 'MSG_DENIED: ' + `stat` 133 if stat != MSG_ACCEPTED:
135 if stat <> MSG_ACCEPTED: 134 raise RuntimeError('Neither MSG_DENIED nor MSG_ACCEPTED: ' + repr(stat))
136 raise RuntimeError, \
137 'Neither MSG_DENIED nor MSG_ACCEPTED: ' + `stat`
138 verf = self.unpack_auth() 135 verf = self.unpack_auth()
139 stat = self.unpack_enum() 136 stat = self.unpack_enum()
140 if stat == PROG_UNAVAIL: 137 if stat == PROG_UNAVAIL:
141 raise RuntimeError, 'call failed: PROG_UNAVAIL' 138 raise RuntimeError('call failed: PROG_UNAVAIL')
142 if stat == PROG_MISMATCH: 139 if stat == PROG_MISMATCH:
143 low = self.unpack_uint() 140 low = self.unpack_uint()
144 high = self.unpack_uint() 141 high = self.unpack_uint()
145 raise RuntimeError, \ 142 raise RuntimeError('call failed: PROG_MISMATCH: ' + repr((low, high)))
146 'call failed: PROG_MISMATCH: ' + `low, high`
147 if stat == PROC_UNAVAIL: 143 if stat == PROC_UNAVAIL:
148 raise RuntimeError, 'call failed: PROC_UNAVAIL' 144 raise RuntimeError('call failed: PROC_UNAVAIL')
149 if stat == GARBAGE_ARGS: 145 if stat == GARBAGE_ARGS:
150 raise RuntimeError, 'call failed: GARBAGE_ARGS' 146 raise RuntimeError('call failed: GARBAGE_ARGS')
151 if stat <> SUCCESS: 147 if stat != SUCCESS:
152 raise RuntimeError, 'call failed: ' + `stat` 148 raise RuntimeError('call failed: ' + repr(stat))
153 return xid, verf 149 return xid, verf
154 # Caller must get procedure-specific part of reply 150 # Caller must get procedure-specific part of reply
155 151
156 152
157 # Subroutines to create opaque authentication objects 153 # Subroutines to create opaque authentication objects
198 offset, ss = divmod(ss + offset, 60) 194 offset, ss = divmod(ss + offset, 60)
199 offset, mm = divmod(mm + offset, 60) 195 offset, mm = divmod(mm + offset, 60)
200 offset, hh = divmod(hh + offset, 24) 196 offset, hh = divmod(hh + offset, 24)
201 d = d + offset 197 d = d + offset
202 _unix_epoch = time.mktime((y, m, d, hh, mm, ss, 0, 0, 0)) 198 _unix_epoch = time.mktime((y, m, d, hh, mm, ss, 0, 0, 0))
203 print "Unix epoch:", time.ctime(_unix_epoch) 199 print("Unix epoch:", time.ctime(_unix_epoch))
204 return _unix_epoch 200 return _unix_epoch
205 201
206 202
207 # Common base class for clients 203 # Common base class for clients
208 204
224 def close(self): 220 def close(self):
225 self.sock.close() 221 self.sock.close()
226 222
227 def makesocket(self): 223 def makesocket(self):
228 # This MUST be overridden 224 # This MUST be overridden
229 raise RuntimeError, 'makesocket not defined' 225 raise RuntimeError('makesocket not defined')
230 226
231 def connsocket(self): 227 def connsocket(self):
232 # Override this if you don't want/need a connection 228 # Override this if you don't want/need a connection
233 self.sock.connect((self.host, self.port)) 229 self.sock.connect((self.host, self.port))
234 230
243 239
244 def make_call(self, proc, args, pack_func, unpack_func): 240 def make_call(self, proc, args, pack_func, unpack_func):
245 #print "make_call() args = " + str(args) 241 #print "make_call() args = " + str(args)
246 # Don't normally override this (but see Broadcast) 242 # Don't normally override this (but see Broadcast)
247 if pack_func is None and args is not None: 243 if pack_func is None and args is not None:
248 raise TypeError, 'non-null args with null pack_func' 244 raise TypeError('non-null args with null pack_func')
249 #print "packed args" 245 #print "packed args"
250 self.start_call(proc) 246 self.start_call(proc)
251 if pack_func: 247 if pack_func:
252 pack_func(args) 248 pack_func(args)
253 self.do_call() 249 self.do_call()
268 p.reset() 264 p.reset()
269 p.pack_callheader(xid, self.prog, self.vers, proc, cred, verf) 265 p.pack_callheader(xid, self.prog, self.vers, proc, cred, verf)
270 266
271 def do_call(self): 267 def do_call(self):
272 # This MUST be overridden 268 # This MUST be overridden
273 raise RuntimeError, 'do_call not defined' 269 raise RuntimeError('do_call not defined')
274 270
275 def mkcred(self): 271 def mkcred(self):
276 # Override this to use more powerful credentials 272 # Override this to use more powerful credentials
277 if self.cred == None: 273 if self.cred == None:
278 self.cred = (AUTH_NULL, make_auth_null()) 274 self.cred = (AUTH_NULL, make_auth_null())
295 except: 291 except:
296 _select=None 292 _select=None
297 293
298 def sendfrag_with_timeout(sock, last, frag, timeout_seconds=None): 294 def sendfrag_with_timeout(sock, last, frag, timeout_seconds=None):
299 x = len(frag) 295 x = len(frag)
300 if last: x = x | 0x80000000L 296 if last: x = x | 0x80000000
301 header = (chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \ 297 header = (chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \
302 chr(int(x>>8 & 0xff)) + chr(int(x & 0xff))) 298 chr(int(x>>8 & 0xff)) + chr(int(x & 0xff)))
303 block=header+frag 299 block=header+frag
304 n=len(block) 300 n=len(block)
305 nsent=0 301 nsent=0
306 while(nsent<n): 302 while(nsent<n):
307 if _select and timeout_seconds: 303 if _select and timeout_seconds:
308 rlist, wlist, xlist=_select([],[sock],[], timeout_seconds) 304 rlist, wlist, xlist=_select([],[sock],[], timeout_seconds)
309 if not wlist: 305 if not wlist:
310 raise EOFError, "Blocked write in sendfrag()" 306 raise EOFError("Blocked write in sendfrag()")
311 nsent+=sock.send(block[nsent:]) 307 nsent+=sock.send(block[nsent:])
312 308
313 def recvfrag_with_timeout(sock, timeout_seconds=None): 309 def recvfrag_with_timeout(sock, timeout_seconds=None):
314 #print "receiving from ", sock 310 #print "receiving from ", sock
315 if _select and timeout_seconds: 311 if _select and timeout_seconds:
316 #print "Selecting with timeout...", timeout_seconds 312 #print "Selecting with timeout...", timeout_seconds
317 rlist, wlist, xlist=_select([sock],[],[], timeout_seconds) 313 rlist, wlist, xlist=_select([sock],[],[], timeout_seconds)
318 if not rlist: 314 if not rlist:
319 raise EOFError, "No header at all in recvfrag()" 315 raise EOFError("No header at all in recvfrag()")
320 316
321 header = sock.recv(4) 317 header = sock.recv(4)
322 if len(header) < 4: 318 if len(header) < 4:
323 raise EOFError 319 raise EOFError
324 x = long(ord(header[0]))<<24 | ord(header[1])<<16 | \ 320 x = int(ord(header[0]))<<24 | ord(header[1])<<16 | \
325 ord(header[2])<<8 | ord(header[3]) 321 ord(header[2])<<8 | ord(header[3])
326 last = ((x & 0x80000000L) != 0) 322 last = ((x & 0x80000000) != 0)
327 n = int(x & 0x7fffffff) 323 n = int(x & 0x7fffffff)
328 324
329 frag='' 325 frag=''
330 326
331 while(len(frag) < n): 327 while(len(frag) < n):
332 if _select and timeout_seconds: 328 if _select and timeout_seconds:
333 #print "Selecting with timeout...", timeout_seconds 329 #print "Selecting with timeout...", timeout_seconds
334 rlist, wlist, xlist=_select([sock],[],[], timeout_seconds) 330 rlist, wlist, xlist=_select([sock],[],[], timeout_seconds)
335 if not rlist: 331 if not rlist:
336 raise EOFError, "No data after header in recvfrag()" 332 raise EOFError("No data after header in recvfrag()")
337 frag += sock.recv(n-len(frag)) 333 frag += sock.recv(n-len(frag))
338 334
339 return last, frag 335 return last, frag
340 336
341 337
359 global last_resv_port_tried 355 global last_resv_port_tried
360 FIRST, LAST = 600, 1024 # Range of ports to try 356 FIRST, LAST = 600, 1024 # Range of ports to try
361 if last_resv_port_tried == None: 357 if last_resv_port_tried == None:
362 import os 358 import os
363 last_resv_port_tried = FIRST + os.getpid() % (LAST-FIRST) 359 last_resv_port_tried = FIRST + os.getpid() % (LAST-FIRST)
364 for i in range(last_resv_port_tried, LAST) + \ 360 for i in list(range(last_resv_port_tried, LAST)) + \
365 range(FIRST, last_resv_port_tried): 361 list(range(FIRST, last_resv_port_tried)):
366 last_resv_port_tried = i 362 last_resv_port_tried = i
367 try: 363 try:
368 sock.bind((host, i)) 364 sock.bind((host, i))
369 return last_resv_port_tried 365 return last_resv_port_tried
370 except socket.error, (errno, msg): 366 except socket.error as xxx_todo_changeme:
371 if errno <> 114: 367 (errno, msg) = xxx_todo_changeme.args
372 raise socket.error, (errno, msg) 368 if errno != 114:
373 raise RuntimeError, 'can\'t assign reserved port' 369 raise socket.error(errno, msg)
370 raise RuntimeError('can\'t assign reserved port')
374 371
375 372
376 # Client using TCP to a specific port 373 # Client using TCP to a specific port
377 374
378 class RawTCPClient(Client): 375 class RawTCPClient(Client):
387 sendrecord(self.sock, call, self.select_timeout_seconds) 384 sendrecord(self.sock, call, self.select_timeout_seconds)
388 reply = recvrecord(self.sock, self.select_timeout_seconds) 385 reply = recvrecord(self.sock, self.select_timeout_seconds)
389 u = self.unpacker 386 u = self.unpacker
390 u.reset(reply) 387 u.reset(reply)
391 xid, verf = u.unpack_replyheader() 388 xid, verf = u.unpack_replyheader()
392 if xid <> self.lastxid: 389 if xid != self.lastxid:
393 # Can't really happen since this is TCP... 390 # Can't really happen since this is TCP...
394 raise RuntimeError, 'wrong xid in reply ' + `xid` + \ 391 raise RuntimeError('wrong xid in reply ' + repr(xid) + \
395 ' instead of ' + `self.lastxid` 392 ' instead of ' + repr(self.lastxid))
396 393
397 394
398 # Client using UDP to a specific port 395 # Client using UDP to a specific port
399 396
400 class RawUDPClient(Client): 397 class RawUDPClient(Client):
406 call = self.packer.get_buf() 403 call = self.packer.get_buf()
407 self.sock.send(call) 404 self.sock.send(call)
408 try: 405 try:
409 from select import select 406 from select import select
410 except ImportError: 407 except ImportError:
411 print 'WARNING: select not found, RPC may hang' 408 print('WARNING: select not found, RPC may hang')
412 select = None 409 select = None
413 BUFSIZE = 8192 # Max UDP buffer size 410 BUFSIZE = 8192 # Max UDP buffer size
414 timeout = 1 411 timeout = 1
415 count = 5 412 count = 5
416 while 1: 413 while 1:
417 r, w, x = [self.sock], [], [] 414 r, w, x = [self.sock], [], []
418 if select: 415 if select:
419 r, w, x = select(r, w, x, timeout) 416 r, w, x = select(r, w, x, timeout)
420 if self.sock not in r: 417 if self.sock not in r:
421 count = count - 1 418 count = count - 1
422 if count < 0: raise RuntimeError, 'timeout' 419 if count < 0: raise RuntimeError('timeout')
423 if timeout < 25: timeout = timeout *2 420 if timeout < 25: timeout = timeout *2
424 ## print 'RESEND', timeout, count 421 ## print 'RESEND', timeout, count
425 self.sock.send(call) 422 self.sock.send(call)
426 continue 423 continue
427 reply = self.sock.recv(BUFSIZE) 424 reply = self.sock.recv(BUFSIZE)
428 u = self.unpacker 425 u = self.unpacker
429 u.reset(reply) 426 u.reset(reply)
430 xid, verf = u.unpack_replyheader() 427 xid, verf = u.unpack_replyheader()
431 if xid <> self.lastxid: 428 if xid != self.lastxid:
432 ## print 'BAD xid' 429 ## print 'BAD xid'
433 continue 430 continue
434 break 431 break
435 432
436 433
453 def set_timeout(self, timeout): 450 def set_timeout(self, timeout):
454 self.timeout = timeout # Use None for infinite timeout 451 self.timeout = timeout # Use None for infinite timeout
455 452
456 def make_call(self, proc, args, pack_func, unpack_func): 453 def make_call(self, proc, args, pack_func, unpack_func):
457 if pack_func is None and args is not None: 454 if pack_func is None and args is not None:
458 raise TypeError, 'non-null args with null pack_func' 455 raise TypeError('non-null args with null pack_func')
459 self.start_call(proc) 456 self.start_call(proc)
460 if pack_func: 457 if pack_func:
461 pack_func(args) 458 pack_func(args)
462 call = self.packer.get_buf() 459 call = self.packer.get_buf()
463 self.sock.sendto(call, (self.host, self.port)) 460 self.sock.sendto(call, (self.host, self.port))
464 try: 461 try:
465 from select import select 462 from select import select
466 except ImportError: 463 except ImportError:
467 print 'WARNING: select not found, broadcast will hang' 464 print('WARNING: select not found, broadcast will hang')
468 select = None 465 select = None
469 BUFSIZE = 8192 # Max UDP buffer size (for reply) 466 BUFSIZE = 8192 # Max UDP buffer size (for reply)
470 replies = [] 467 replies = []
471 if unpack_func is None: 468 if unpack_func is None:
472 def dummy(): pass 469 def dummy(): pass
482 break 479 break
483 reply, fromaddr = self.sock.recvfrom(BUFSIZE) 480 reply, fromaddr = self.sock.recvfrom(BUFSIZE)
484 u = self.unpacker 481 u = self.unpacker
485 u.reset(reply) 482 u.reset(reply)
486 xid, verf = u.unpack_replyheader() 483 xid, verf = u.unpack_replyheader()
487 if xid <> self.lastxid: 484 if xid != self.lastxid:
488 ## print 'BAD xid' 485 ## print 'BAD xid'
489 continue 486 continue
490 reply = unpack_func() 487 reply = unpack_func()
491 self.unpacker.done() 488 self.unpacker.done()
492 replies.append((reply, fromaddr)) 489 replies.append((reply, fromaddr))
625 portmap_proxy_host=host #use a proxy to get around firewalled portmappers 622 portmap_proxy_host=host #use a proxy to get around firewalled portmappers
626 pmap = TCPPortMapperClient(portmap_proxy_host, portmap_proxy_port,timeout_seconds) 623 pmap = TCPPortMapperClient(portmap_proxy_host, portmap_proxy_port,timeout_seconds)
627 port = pmap.Getport((prog, vers, IPPROTO_TCP, 0)) 624 port = pmap.Getport((prog, vers, IPPROTO_TCP, 0))
628 pmap.close() 625 pmap.close()
629 if port == 0: 626 if port == 0:
630 raise RuntimeError, 'program not registered' 627 raise RuntimeError('program not registered')
631 RawTCPClient.__init__(self, host, prog, vers, port) 628 RawTCPClient.__init__(self, host, prog, vers, port)
632 629
633 630
634 class UDPClient(RawUDPClient): 631 class UDPClient(RawUDPClient):
635 632
638 portmap_proxy_host=host #use a proxy to get around firewalled portmappers 635 portmap_proxy_host=host #use a proxy to get around firewalled portmappers
639 pmap = UDPPortMapperClient(portmap_proxy_host, portmap_proxy_port) 636 pmap = UDPPortMapperClient(portmap_proxy_host, portmap_proxy_port)
640 port = pmap.Getport((prog, vers, IPPROTO_UDP, 0)) 637 port = pmap.Getport((prog, vers, IPPROTO_UDP, 0))
641 pmap.close() 638 pmap.close()
642 if port == 0: 639 if port == 0:
643 raise RuntimeError, 'program not registered' 640 raise RuntimeError('program not registered')
644 RawUDPClient.__init__(self, host, prog, vers, port) 641 RawUDPClient.__init__(self, host, prog, vers, port)
645 642
646 643
647 class BroadcastUDPClient(Client): 644 class BroadcastUDPClient(Client):
648 645
711 self.unpacker.reset(call) 708 self.unpacker.reset(call)
712 self.packer.reset() 709 self.packer.reset()
713 xid = self.unpacker.unpack_uint() 710 xid = self.unpacker.unpack_uint()
714 self.packer.pack_uint(xid) 711 self.packer.pack_uint(xid)
715 temp = self.unpacker.unpack_enum() 712 temp = self.unpacker.unpack_enum()
716 if temp <> CALL: 713 if temp != CALL:
717 return None # Not worthy of a reply 714 return None # Not worthy of a reply
718 self.packer.pack_uint(REPLY) 715 self.packer.pack_uint(REPLY)
719 temp = self.unpacker.unpack_uint() 716 temp = self.unpacker.unpack_uint()
720 if temp <> RPCVERSION: 717 if temp != RPCVERSION:
721 self.packer.pack_uint(MSG_DENIED) 718 self.packer.pack_uint(MSG_DENIED)
722 self.packer.pack_uint(RPC_MISMATCH) 719 self.packer.pack_uint(RPC_MISMATCH)
723 self.packer.pack_uint(RPCVERSION) 720 self.packer.pack_uint(RPCVERSION)
724 self.packer.pack_uint(RPCVERSION) 721 self.packer.pack_uint(RPCVERSION)
725 return self.packer.get_buf() 722 return self.packer.get_buf()
726 self.packer.pack_uint(MSG_ACCEPTED) 723 self.packer.pack_uint(MSG_ACCEPTED)
727 self.packer.pack_auth((AUTH_NULL, make_auth_null())) 724 self.packer.pack_auth((AUTH_NULL, make_auth_null()))
728 prog = self.unpacker.unpack_uint() 725 prog = self.unpacker.unpack_uint()
729 if prog <> self.prog: 726 if prog != self.prog:
730 self.packer.pack_uint(PROG_UNAVAIL) 727 self.packer.pack_uint(PROG_UNAVAIL)
731 return self.packer.get_buf() 728 return self.packer.get_buf()
732 vers = self.unpacker.unpack_uint() 729 vers = self.unpacker.unpack_uint()
733 if vers <> self.vers: 730 if vers != self.vers:
734 self.packer.pack_uint(PROG_MISMATCH) 731 self.packer.pack_uint(PROG_MISMATCH)
735 self.packer.pack_uint(self.vers) 732 self.packer.pack_uint(self.vers)
736 self.packer.pack_uint(self.vers) 733 self.packer.pack_uint(self.vers)
737 return self.packer.get_buf() 734 return self.packer.get_buf()
738 proc = self.unpacker.unpack_uint() 735 proc = self.unpacker.unpack_uint()
739 methname = 'handle_' + `proc` 736 methname = 'handle_' + repr(proc)
740 try: 737 try:
741 meth = getattr(self, methname) 738 meth = getattr(self, methname)
742 except AttributeError: 739 except AttributeError:
743 self.packer.pack_uint(PROC_UNAVAIL) 740 self.packer.pack_uint(PROC_UNAVAIL)
744 return self.packer.get_buf() 741 return self.packer.get_buf()
766 def handle_0(self): # Handle NULL message 763 def handle_0(self): # Handle NULL message
767 self.turn_around() 764 self.turn_around()
768 765
769 def makesocket(self): 766 def makesocket(self):
770 # This MUST be overridden 767 # This MUST be overridden
771 raise RuntimeError, 'makesocket not defined' 768 raise RuntimeError('makesocket not defined')
772 769
773 def bindsocket(self): 770 def bindsocket(self):
774 # Override this to bind to a different port (e.g. reserved) 771 # Override this to bind to a different port (e.g. reserved)
775 self.sock.bind((self.host, self.port)) 772 self.sock.bind((self.host, self.port))
776 773
784 781
785 def register(self): 782 def register(self):
786 mapping = self.prog, self.vers, self.prot, self.port 783 mapping = self.prog, self.vers, self.prot, self.port
787 p = TCPPortMapperClient(self.host) 784 p = TCPPortMapperClient(self.host)
788 if not p.Set(mapping): 785 if not p.Set(mapping):
789 raise RuntimeError, 'register failed' 786 raise RuntimeError('register failed')
790 787
791 def unregister(self): 788 def unregister(self):
792 mapping = self.prog, self.vers, self.prot, self.port 789 mapping = self.prog, self.vers, self.prot, self.port
793 p = TCPPortMapperClient(self.host) 790 p = TCPPortMapperClient(self.host)
794 if not p.Unset(mapping): 791 if not p.Unset(mapping):
795 raise RuntimeError, 'unregister failed' 792 raise RuntimeError('unregister failed')
796 793
797 def makesocket(self): 794 def makesocket(self):
798 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 795 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
799 self.prot = IPPROTO_TCP 796 self.prot = IPPROTO_TCP
800 797
808 while 1: 805 while 1:
809 try: 806 try:
810 call = recvrecord(sock) 807 call = recvrecord(sock)
811 except EOFError: 808 except EOFError:
812 break 809 break
813 except socket.error, msg: 810 except socket.error as msg:
814 print 'socket error:', msg 811 print('socket error:', msg)
815 break 812 break
816 reply = self.handle(call) 813 reply = self.handle(call)
817 if reply is not None: 814 if reply is not None:
818 sendrecord(sock, reply) 815 sendrecord(sock, reply)
819 816
850 847
851 def register(self): 848 def register(self):
852 mapping = self.prog, self.vers, self.prot, self.port 849 mapping = self.prog, self.vers, self.prot, self.port
853 p = UDPPortMapperClient(self.host) 850 p = UDPPortMapperClient(self.host)
854 if not p.Set(mapping): 851 if not p.Set(mapping):
855 raise RuntimeError, 'register failed' 852 raise RuntimeError('register failed')
856 853
857 def unregister(self): 854 def unregister(self):
858 mapping = self.prog, self.vers, self.prot, self.port 855 mapping = self.prog, self.vers, self.prot, self.port
859 p = UDPPortMapperClient(self.host) 856 p = UDPPortMapperClient(self.host)
860 if not p.Unset(mapping): 857 if not p.Unset(mapping):
861 raise RuntimeError, 'unregister failed' 858 raise RuntimeError('unregister failed')
862 859
863 def makesocket(self): 860 def makesocket(self):
864 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 861 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
865 self.prot = IPPROTO_UDP 862 self.prot = IPPROTO_UDP
866 863
869 self.session() 866 self.session()
870 867
871 def session(self): 868 def session(self):
872 call, host_port = self.sock.recvfrom(8192) 869 call, host_port = self.sock.recvfrom(8192)
873 reply = self.handle(call) 870 reply = self.handle(call)
874 if reply <> None: 871 if reply != None:
875 self.sock.sendto(reply, host_port) 872 self.sock.sendto(reply, host_port)
876 873
877 874
878 # Simple test program -- dump local portmapper status 875 # Simple test program -- dump local portmapper status
879 876
880 def test(): 877 def test():
881 pmap = UDPPortMapperClient('') 878 pmap = UDPPortMapperClient('')
882 list = pmap.Dump() 879 list = pmap.Dump()
883 list.sort() 880 list.sort()
884 for prog, vers, prot, port in list: 881 for prog, vers, prot, port in list:
885 print prog, vers, 882 print(prog, vers, end=' ')
886 if prot == IPPROTO_TCP: print 'tcp', 883 if prot == IPPROTO_TCP: print('tcp', end=' ')
887 elif prot == IPPROTO_UDP: print 'udp', 884 elif prot == IPPROTO_UDP: print('udp', end=' ')
888 else: print prot, 885 else: print(prot, end=' ')
889 print port 886 print(port)
890 887
891 888
892 # Test program for broadcast operation -- dump everybody's portmapper status 889 # Test program for broadcast operation -- dump everybody's portmapper status
893 890
894 def testbcast(): 891 def testbcast():
897 bcastaddr = sys.argv[1] 894 bcastaddr = sys.argv[1]
898 else: 895 else:
899 bcastaddr = '<broadcast>' 896 bcastaddr = '<broadcast>'
900 def rh(reply, fromaddr): 897 def rh(reply, fromaddr):
901 host, port = fromaddr 898 host, port = fromaddr
902 print host + '\t' + `reply` 899 print(host + '\t' + repr(reply))
903 pmap = BroadcastUDPPortMapperClient(bcastaddr) 900 pmap = BroadcastUDPPortMapperClient(bcastaddr)
904 pmap.set_reply_handler(rh) 901 pmap.set_reply_handler(rh)
905 pmap.set_timeout(5) 902 pmap.set_timeout(5)
906 replies = pmap.Getport((100002, 1, IPPROTO_UDP, 0)) 903 replies = pmap.Getport((100002, 1, IPPROTO_UDP, 0))
907 904
915 # Simple test class -- proc 1 doubles its string argument as reply 912 # Simple test class -- proc 1 doubles its string argument as reply
916 class S(UDPServer): 913 class S(UDPServer):
917 def handle_1(self): 914 def handle_1(self):
918 arg = self.unpacker.unpack_string() 915 arg = self.unpacker.unpack_string()
919 self.turn_around() 916 self.turn_around()
920 print 'RPC function 1 called, arg', `arg` 917 print('RPC function 1 called, arg', repr(arg))
921 self.packer.pack_string(arg + arg) 918 self.packer.pack_string(arg + arg)
922 # 919 #
923 s = S('', 0x20000000, 1, 0) 920 s = S('', 0x20000000, 1, 0)
924 try: 921 try:
925 s.unregister() 922 s.unregister()
926 except RuntimeError, msg: 923 except RuntimeError as msg:
927 print 'RuntimeError:', msg, '(ignored)' 924 print('RuntimeError:', msg, '(ignored)')
928 s.register() 925 s.register()
929 print 'Service started...' 926 print('Service started...')
930 try: 927 try:
931 s.loop() 928 s.loop()
932 finally: 929 finally:
933 s.unregister() 930 s.unregister()
934 print 'Service interrupted.' 931 print('Service interrupted.')
935 932
936 933
937 def testclt(): 934 def testclt():
938 import sys 935 import sys
939 if sys.argv[1:]: host = sys.argv[1] 936 if sys.argv[1:]: host = sys.argv[1]
943 def call_1(self, arg): 940 def call_1(self, arg):
944 return self.make_call(1, arg, \ 941 return self.make_call(1, arg, \
945 self.packer.pack_string, \ 942 self.packer.pack_string, \
946 self.unpacker.unpack_string) 943 self.unpacker.unpack_string)
947 c = C(host, 0x20000000, 1) 944 c = C(host, 0x20000000, 1)
948 print 'making call...' 945 print('making call...')
949 reply = c.call_1('hello, world, ') 946 reply = c.call_1('hello, world, ')
950 print 'call returned', `reply` 947 print('call returned', repr(reply))
951 948
952 def testclt2(): 949 def testclt2():
953 import sys 950 import sys
954 host = '127.0.0.1' 951 host = '127.0.0.1'
955 # Client for above server 952 # Client for above server
957 def call_1(self, arg): 954 def call_1(self, arg):
958 return self.make_call(1, arg, \ 955 return self.make_call(1, arg, \
959 self.packer.pack_string, \ 956 self.packer.pack_string, \
960 self.unpacker.unpack_string) 957 self.unpacker.unpack_string)
961 c = C(host, 0x20000000, 1) 958 c = C(host, 0x20000000, 1)
962 print 'making call...' 959 print('making call...')
963 reply = c.call_1('hello, world, ') 960 reply = c.call_1('hello, world, ')
964 print 'call returned', `reply` 961 print('call returned', repr(reply))
965 962