home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / rpc / rnusersclient.py < prev    next >
Text File  |  1993-12-17  |  2KB  |  99 lines

  1. # Remote nusers client interface
  2.  
  3. import rpc
  4. from rpc import Packer, Unpacker, UDPClient, BroadcastUDPClient
  5.  
  6.  
  7. class RnusersPacker(Packer):
  8.     def pack_utmp(self, ui):
  9.         ut_line, ut_name, ut_host, ut_time = utmp
  10.         self.pack_string(ut_line)
  11.         self.pack_string(ut_name)
  12.         self.pack_string(ut_host)
  13.         self.pack_int(ut_time)
  14.     def pack_utmpidle(self, ui):
  15.         ui_itmp, ui_idle = ui
  16.         self.pack_utmp(ui_utmp)
  17.         self.pack_uint(ui_idle)
  18.     def pack_utmpidlearr(self, list):
  19.         self.pack_array(list, self.pack_itmpidle)
  20.  
  21.  
  22. class RnusersUnpacker(Unpacker):
  23.     def unpack_utmp(self):
  24.         ut_line = self.unpack_string()
  25.         ut_name = self.unpack_string()
  26.         ut_host = self.unpack_string()
  27.         ut_time = self.unpack_int()
  28.         return ut_line, ut_name, ut_host, ut_time
  29.     def unpack_utmpidle(self):
  30.         ui_utmp = self.unpack_utmp()
  31.         ui_idle = self.unpack_uint()
  32.         return ui_utmp, ui_idle
  33.     def unpack_utmpidlearr(self):
  34.         return self.unpack_array(self.unpack_utmpidle)
  35.  
  36.  
  37. class PartialRnusersClient:
  38.  
  39.     def addpackers(self):
  40.         self.packer = RnusersPacker()
  41.         self.unpacker = RnusersUnpacker('')
  42.  
  43.     def Num(self):
  44.         return self.make_call(1, None, None, self.unpacker.unpack_int)
  45.  
  46.     def Names(self):
  47.         return self.make_call(2, None, \
  48.             None, self.unpacker.unpack_utmpidlearr)
  49.  
  50.     def Allnames(self):
  51.         return self.make_call(3, None, \
  52.             None, self.unpacker.unpack_utmpidlearr)
  53.  
  54.  
  55. class RnusersClient(PartialRnusersClient, UDPClient):
  56.  
  57.     def __init__(self, host):
  58.         UDPClient.__init__(self, host, 100002, 2)
  59.  
  60.  
  61. class BroadcastRnusersClient(PartialRnusersClient, BroadcastUDPClient):
  62.  
  63.     def __init__(self, bcastaddr):
  64.         BroadcastUDPClient.__init__(self, bcastaddr, 100002, 2)
  65.  
  66.  
  67. def test():
  68.     import sys
  69.     if not sys.argv[1:]:
  70.         testbcast()
  71.         return
  72.     else:
  73.         host = sys.argv[1]
  74.     c = RnusersClient(host)
  75.     list = c.Names()
  76.     for (line, name, host, time), idle in list:
  77.         line = strip0(line)
  78.         name = strip0(name)
  79.         host = strip0(host)
  80.         print `name`, `host`, `line`, time, idle
  81.  
  82. def testbcast():
  83.     c = BroadcastRnusersClient('<broadcast>')
  84.     def listit(list, fromaddr):
  85.         host, port = fromaddr
  86.         print host + '\t:',
  87.         for (line, name, host, time), idle in list:
  88.             print strip0(name),
  89.         print
  90.     c.set_reply_handler(listit)
  91.     all = c.Names()
  92.     print 'Total Count:', len(all)
  93.  
  94. def strip0(s):
  95.     while s and s[-1] == '\0': s = s[:-1]
  96.     return s
  97.  
  98. test()
  99.