home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / TeamSpeak / TeamSpeak3-Client-win32-3.0.0.exe / plugins / clientquery_plugin / examples / clientquery.py next >
Text File  |  2011-06-24  |  2KB  |  101 lines

  1. #
  2. # TeamSpeak 3 ClientQuery example
  3. # Copyright (c) 2010 TeamSpeak Systems GmbH
  4. #
  5. # Common clientquery functions
  6. #
  7.  
  8. import sys, socket
  9.  
  10. #
  11. # Get value by key from "key=value" string
  12. #
  13. def getParamValue(data, key):
  14.     datas = data.split()
  15.     for data in datas:
  16.         s = data.split('=', 1)
  17.         if s[0] == key:
  18.             return s[1]
  19.     return None
  20.  
  21. #
  22. # Build "key=value" string from list of tuples
  23. #
  24. def buildParam(datas):
  25.     param = ''
  26.     for data in datas:
  27.         if not data:
  28.             break
  29.         param += '%s=%s' % (data[0], data[1])
  30.     return param
  31.  
  32. #
  33. # Check received data for error message
  34. # Return None if no error occured. Otherwise return a tuple (errorID, errorMessage).
  35. #
  36. def checkError(data):
  37.     if not data[0].startswith('error '):
  38.         return None
  39.     e = data[0].split()
  40.     id = int(getParamValue(e[1], 'id'))
  41.     if id == 0:  # ERROR_ok == 0
  42.         return None
  43.     msg = getParamValue(e[2], 'msg').replace('\s', ' ')
  44.     print 'Error: %s (%d)' % (msg, id)
  45.     return (id, msg)
  46.  
  47. #
  48. # Receive data from socket and check received lines if an error occured.
  49. # Return a tuple (list of messages, (errorID, errorMessage))
  50. # If no error occured, return (list of messages, None)
  51. #
  52. def receive(s):
  53.     lines = []
  54.     data = ''
  55.     while True:
  56.         try:
  57.             d = s.recv(1024)
  58.         except socket.error, err:
  59.             if type(err) != socket.timeout:
  60.                 print 'Error receiving from server: %s' % err
  61.                 sys.exit(0)
  62.             break
  63.         if not d:
  64.             continue
  65.         data += d
  66.         while '\n\r' in data:
  67.             datas = data.split('\n\r', 1)
  68.             lines.append(datas[0])
  69.             print '<<', datas[0]
  70.             data = datas[1]
  71.         if len(lines) > 0:
  72.             last = lines[len(lines)-1]
  73.             if last.startswith('error '):
  74.                 break  # Avoid waiting for timeout as the last line usually is an error message
  75.             elif len(lines) == 3 and last.startswith('selected schandlerid=1'):
  76.                 break  # Avoid waiting for timeout on connect message
  77.     if len(lines) == 0:
  78.         return (None, None)
  79.     return (lines, checkError(lines))
  80.  
  81. #
  82. # Send data to socket
  83. #
  84. def send(s, data):
  85.     print '>>', data
  86.     s.send(data + '\r\n')
  87.  
  88. #
  89. # Demonstrate connecting to a local TeamSpeak 3 clientquery
  90. #
  91. def connectTS3Query(host, port):
  92.     # Create TCP socket and connect to clientquery on localhost:25639
  93.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  94.     try:
  95.         s.connect((host, port))
  96.     except socket.error:
  97.         print 'Failed to connect to %s:%d' % (host, port)
  98.         return None
  99.     s.settimeout(0.5)
  100.     return s
  101.