rnusersclient.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Demo » rpc » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Demo » rpc » rnusersclient.py
# Remote nusers client interface

import rpc
from rpc import Packer,Unpacker,UDPClient,BroadcastUDPClient


class RnusersPacker(Packer):
  def pack_utmp(self, ui):
    ut_line, ut_name, ut_host, ut_time = utmp
    self.pack_string(ut_line)
    self.pack_string(ut_name)
    self.pack_string(ut_host)
    self.pack_int(ut_time)
  def pack_utmpidle(self, ui):
    ui_itmp, ui_idle = ui
    self.pack_utmp(ui_utmp)
    self.pack_uint(ui_idle)
  def pack_utmpidlearr(self, list):
    self.pack_array(list, self.pack_itmpidle)


class RnusersUnpacker(Unpacker):
  def unpack_utmp(self):
    ut_line = self.unpack_string()
    ut_name = self.unpack_string()
    ut_host = self.unpack_string()
    ut_time = self.unpack_int()
    return ut_line, ut_name, ut_host, ut_time
  def unpack_utmpidle(self):
    ui_utmp = self.unpack_utmp()
    ui_idle = self.unpack_uint()
    return ui_utmp, ui_idle
  def unpack_utmpidlearr(self):
    return self.unpack_array(self.unpack_utmpidle)


class PartialRnusersClient:

  def addpackers(self):
    self.packer = RnusersPacker()
    self.unpacker = RnusersUnpacker('')

  def Num(self):
    return self.make_call(1, None, None, self.unpacker.unpack_int)

  def Names(self):
    return self.make_call(2, None, \
      None, self.unpacker.unpack_utmpidlearr)

  def Allnames(self):
    return self.make_call(3, None, \
      None, self.unpacker.unpack_utmpidlearr)


class RnusersClient(PartialRnusersClient, UDPClient):

  def __init__(self, host):
    UDPClient.__init__(self, host, 100002, 2)


class BroadcastRnusersClient(PartialRnusersClient, BroadcastUDPClient):

  def __init__(self, bcastaddr):
    BroadcastUDPClient.__init__(self, bcastaddr, 100002, 2)


def test():
  import sys
  if not sys.argv[1:]:
    testbcast()
    return
  else:
    host = sys.argv[1]
  c = RnusersClient(host)
  list = c.Names()
  for (line, name, host, time), idle in list:
    line = strip0(line)
    name = strip0(name)
    host = strip0(host)
    print `name`, `host`, `line`, time, idle

def testbcast():
  c = BroadcastRnusersClient('<broadcast>')
  def listit(list, fromaddr):
    host, port = fromaddr
    print host + '\t:',
    for (line, name, host, time), idle in list:
      print strip0(name),
    print
  c.set_reply_handler(listit)
  all = c.Names()
  print 'Total Count:', len(all)

def strip0(s):
  while s and s[-1] == '\0': s = s[:-1]
  return s

test()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.