Changeset 5
- Timestamp:
- 03/21/05 11:41:16 (19 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/daemon/togad.py
r4 r5 1 1 #!/usr/bin/env python 2 2 3 import xml.sax 3 from xml.sax import make_parser 4 from xml.sax.handler import ContentHandler 5 import socket 6 import sys 4 7 5 class ContentGenerator( xml.sax.handler.ContentHandler ): 8 class GangliaXMLHandler(ContentHandler): 9 """ 10 Parse/Handle XML 11 """ 6 12 7 def __init__(self, out = sys.stdout): 8 handler.ContentHandler.__init__(self) 9 self._out = out 13 def __init__ (self, searchTerm): 14 self.isHostElement, self.isxReboundsElement = 0, 0; 15 16 def startElement(self, name, attrs): 10 17 11 # ContentHandler methods 12 13 def startDocument(self): 14 self._out.write('<?xml version="1.0" encoding="iso-8859-1"?>\n') 18 if name == 'player': 19 self.playerName = attrs.get('name',"") 20 self.playerAge = attrs.get('age',"") 21 self.playerHeight = attrs.get('height',"") 22 elif name == 'points': 23 self.isPointsElement= 1; 24 self.playerPoints = ""; 25 elif name == 'rebounds': 26 self.isReboundsElement = 1; 27 self.playerRebounds = ""; 28 return 15 29 16 def startElement(self, name, attrs):17 self._out.write('<' + name) 18 for (name, value) in attrs.items(): 19 self._out.write(' %s="%s"' % (name, saxutils.escape(value))) 20 self._out.write('>') 30 def characters (self, ch): 31 if self.isPointsElement== 1: 32 self.playerPoints += ch 33 if self.isReboundsElement == 1: 34 self.playerRebounds += ch 21 35 22 def endElement(self, name): 23 self._out.write('</%s>' % name) 36 def endElement(self, name): 37 if name == 'points': 38 self.isPointsElement= 0 39 if name == 'rebounds': 40 self.inPlayersContent = 0 41 if name == 'player' and self.searchTerm== self.playerName : 42 print '<h2>Statistics for player:' , self.playerName, '</h2><br>(age:', self.playerAge , 'height' , self.playerHeight , ")<br>" 43 print 'Match average:', self.playerPoints , 'points,' , self.playerRebounds, 'rebounds' 24 44 25 def characters(self, content): 26 self._out.write(saxutils.escape(content)) 45 class GangliaXMLGatherer: 46 """ 47 Connect to a gmetad and return fd 48 """ 27 49 28 def ignorableWhitespace(self, content): 29 self._out.write(content) 30 31 def processingInstruction(self, target, data): 32 self._out.write('<?%s %s?>' % (target, data)) 50 def __init__(self, host, port): 51 self.host = host 52 self.port = port 33 53 34 # --- The main program 54 def getFileDescriptor(self): 55 s = None 56 for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM): 57 af, socktype, proto, canonname, sa = res 58 try: 59 s = socket.socket(af, socktype, proto) 60 except socket.error, msg: 61 s = None 62 continue 63 try: 64 s.connect(sa) 65 except socket.error, msg: 66 s.close() 67 s = None 68 continue 69 break 35 70 36 parser = xml.sax.make_parser() 37 parser.setContentHandler(ContentGenerator()) 38 parser.parse(sys.argv[1]) 71 if s is None: 72 print 'could not open socket' 73 sys.exit(1) 74 75 return s.fileno() 76 77 #s.send('Hello, world') 78 #data = s.recv(1024) 79 #s.close() 80 #print 'Received', `data` 81 82 def main(): 83 """ 84 My Main 85 """ 86 87 myXMLGatherer = GangliaXMLGatherer( localhost, 8651 ) 88 89 myParser = make_parser() 90 myHandler = GangliaXMLHandler() 91 myParser.setContentHandler( myHandler ) 92 myParser.parse( myXMLGatherer.getFileDescriptor() ) 93 94 # Let's go 95 main()
Note: See TracChangeset
for help on using the changeset viewer.