def do_something(self): if 11 == self._request: self._device_id = self._device elif 10007 == self._request: pass try: body1 = self._body[self._client_header_length:] body_json = json.loads(body1) box_id = body_json["serialid"] re7 = requests.get("http://192.168.1.199:4201/box/info/by/ip/", params={'ip': self._ip}) js7 = re7.json() ver_code = js7["result"]["vercode"] ret_val = {} ret_val["appServerUrl"] = "" ret_val["result"] = {"code": ver_code, "port": 3050} ret_val["status"] = 0 body1 = json.dumps(ret_val) logging.debug(" ret json: %s ret val %s: ", js7, ret_val) parts = unpack('6I', self._body[:self._client_header_length]) parts = [ntohl(x) for x in parts] parts[4] = len(body1) info_header = pack("6I", htonl(parts[0]), htonl(parts[1]), htonl(parts[2]), htonl(parts[3]), htonl(parts[4]), htonl(parts[5])) packet_body = info_header + body1 self._body = packet_body self._length = len(packet_body) except Exception, e: logging.debug(e)
def send_packet_back(self): from socket import htonl ip = htonl(unpack('I', socket.inet_aton(self._ip))[0]) header = pack("2I32sdII", htonl(self._device_type), htonl(self._device_id), self._md5, self._timestamp, htonl(len(self._body)), ip) parts = unpack('6I', self._body[:self._client_header_length]) body1 = self._body[self._client_header_length:] parts = [ntohl(x) for x in parts] parts[5] = self._device parts[4] = len(body1) send_header = pack("6I", htonl(parts[0]), htonl(parts[1]), htonl(parts[2]), htonl(parts[3]), htonl(parts[4]), htonl(parts[5])) msg = header + send_header + body1 self._stream.write(msg) if parts[2] != 11: logging.debug('send packet back: header(%d, %d, %s, %.4f, %d, %s)' % (self._device_type, self._device_id, self._md5, self._timestamp, len(self._body), self._ip)) logging.debug('send body: header(%d, %d, %d, %d, %d, %d) body:%s' % (parts[0], parts[1], parts[2], parts[3], parts[4], parts[5],body1)) self._stream.read_bytes(BUSINESS_HEADER_LENGTH, self.read_packet_header) pass