def readHeader(self, socket, printDebug = False): header = "" numNLs = 0 retrys = 32 headerLen = 0 if self.log: self.log.write("Waiting for msg.\n") while numNLs < 3: try: if retrys <= 0: if self.log: self.log.write("Error:Bad header. Returning 0\n") return 0 char = socket.recv(1) except KeyboardInterrupt: if self.log: self.log.write("Error:Interrupted waiting for Msg\n") raise XoomWaitMsgKeyboardInterrupt if char == "": self.choutfl('.') time.sleep(1) elif printDebug: self.chout(char) headerLen += 1 if char == "\n": numNLs += 1 header += char if char == "": retrys += -1 headerInfo = header.split('\n') if headerInfo[0] <> "XTP/1.0": if self.log: self.log.write("Error:Bad header - '%s'\n" %headerInfo[0]); return 0 if headerInfo[1].find("Content-Length") < 0: if self.log: self.log.write("Error:Bad header - '%s'\n" %headerInfo[1]); return 0 if headerInfo[2] <> '': if self.log: self.log.write("Error:Bad header - '%s'\n" %headerInfo[2]); return 0 if headerLen != 35: if self.log: self.log.write("Error:Bad header - '%s'. Length incorrect.\n" \ %header); contentLen = headerInfo[1].split()[1] if self.log: self.log.write("Content length is %s\n" %contentLen) return int(contentLen)
def readXoomMsg(self, socket): try: contentLen = self.readHeader(socket) except: if self.log: self.log.write("Error:Failed to get header: '%s'\n" \ %string.join(traceback.format_exception(*sys.exc_info()), '')) contentLen = 0 data = socket.recv(contentLen) datalen = len(data) count = 1 while datalen < int(contentLen): if self.log: self.log.write("Warn:Received data length %s not correct: %s\n"\ %(datalen, int(contentLen))) data += socket.recv(int(contentLen)-datalen) datalen = len(data) if self.log: self.log.write("Warn:Retry no: %s. Received data length %s\n" \ %(count, datalen)) count += 1 if count > 50: break xmlData = "" try: xmlData = parseString(data) if self.log: self.log.write("Response data is: \n'%s'\n" \ %(xmlData.toprettyxml())) except: if self.log: self.log.write("Error:Failed to parse XML response: '%s'\n" \ %string.join(traceback.format_exception(*sys.exc_info()), '')) raise return xmlData