[3] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[5] | 3 | from xml.sax import make_parser |
---|
| 4 | from xml.sax.handler import ContentHandler |
---|
| 5 | import socket |
---|
| 6 | import sys |
---|
[3] | 7 | |
---|
[5] | 8 | class GangliaXMLHandler(ContentHandler): |
---|
| 9 | """ |
---|
| 10 | Parse/Handle XML |
---|
| 11 | """ |
---|
[3] | 12 | |
---|
[5] | 13 | def __init__ (self, searchTerm): |
---|
| 14 | self.isHostElement, self.isxReboundsElement = 0, 0; |
---|
| 15 | |
---|
| 16 | def startElement(self, name, attrs): |
---|
[3] | 17 | |
---|
[5] | 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 |
---|
[3] | 29 | |
---|
[5] | 30 | def characters (self, ch): |
---|
| 31 | if self.isPointsElement== 1: |
---|
| 32 | self.playerPoints += ch |
---|
| 33 | if self.isReboundsElement == 1: |
---|
| 34 | self.playerRebounds += ch |
---|
[3] | 35 | |
---|
[5] | 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' |
---|
[3] | 44 | |
---|
[5] | 45 | class GangliaXMLGatherer: |
---|
| 46 | """ |
---|
| 47 | Connect to a gmetad and return fd |
---|
| 48 | """ |
---|
[3] | 49 | |
---|
[5] | 50 | def __init__(self, host, port): |
---|
| 51 | self.host = host |
---|
| 52 | self.port = port |
---|
[3] | 53 | |
---|
[5] | 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 |
---|
[3] | 70 | |
---|
[5] | 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() |
---|