#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket, struct
import sys
import traceback
import json
import time

reload(sys)
sys.setdefaultencoding('utf-8')

"""
    ESBoost%d%s: %d msg_len msg
"""

ip = '192.168.86.39'
port = 12711
client_socket = None

def init():
    global client_socket
    
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.settimeout(2) # set time out
    client_socket.connect((ip, port))

def cleanup():
    global client_socket
    
    # before clean up
    print "before doing clean up..."
    if client_socket:
        try:
            client_socket.close()
        except:
            print "catch an exception %s" % (traceback.format_exc())
    # end clean up
    print "end clean up."

if __name__ == '__main__':
    req_str = json.dumps({'usr_id':'zhushou1_iflytek_web', 'category':'qa_chat|qa_datetime|qa_music|qa_forbidden|qa_calc|qa_pattern|qa_baike|qa_faq', 'q':'1+1', 'UUID':'xxxxxxxx'})
    req_head = struct.pack('i', len(req_str))

    start = time.time()
    
    for j in range(1000):
        try:
            ret_val = ""
            init()
            client_socket.send('%s%s' % (req_head, req_str))
            
            buf_len = client_socket.recv(4)
            (msg_len,) = struct.unpack('i', buf_len)
            #print msg_len
            
            while msg_len > 0:
                buf = client_socket.recv(1024)
                #print "msg_len: %d, buf_len: %d, receive message: %s" %(msg_len, len(buf), buf)
                if not len(buf):
                    break
                msg_len -= len(buf)
                ret_val = '%s%s' %(ret_val, buf)
                
            print '%s' %(ret_val)
        except socket.timeout, e:
            print "socket timeout error: %s" %(traceback.format_exc())
        except Exception, e:
            print "socket send and recieve Error: %s" %(traceback.format_exc())
        else:
            cleanup()

    end = time.time()

    print 'eclipse time: %d' %(1000*(end-start))
    #print ret_val
5c8de81d285c587c0ad122775ce04df9aa56e830
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket, struct
import sys
import traceback
import json
import time

reload(sys)
sys.setdefaultencoding('utf-8')

"""
    ESBoost%d%s: %d msg_len msg
"""

ip = '192.168.86.39'
port = 12711
client_socket = None

def init():
    global client_socket
    
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.settimeout(2) # set time out
    client_socket.connect((ip, port))

def cleanup():
    global client_socket
    
    # before clean up
    print "before doing clean up..."
    if client_socket:
        try:
            client_socket.close()
        except:
            print "catch an exception %s" % (traceback.format_exc())
    # end clean up
    print "end clean up."

if __name__ == '__main__':
    req_str = json.dumps({'usr_id':'zhushou1_iflytek_web', 'category':'qa_chat|qa_datetime|qa_music|qa_forbidden|qa_calc|qa_pattern|qa_baike|qa_faq', 'q':'1+1', 'UUID':'xxxxxxxx'})
    req_head = struct.pack('i', len(req_str))

    start = time.time()
    
    for j in range(1000):
        try:
            ret_val = ""
            init()
            client_socket.send('%s%s' % (req_head, req_str))
            
            buf_len = client_socket.recv(4)
            (msg_len,) = struct.unpack('i', buf_len)
            #print msg_len
            
            while msg_len > 0:
                buf = client_socket.recv(1024)
                #print "msg_len: %d, buf_len: %d, receive message: %s" %(msg_len, len(buf), buf)
                if not len(buf):
                    break
                msg_len -= len(buf)
                ret_val = '%s%s' %(ret_val, buf)
                
            print '%s' %(ret_val)
        except socket.timeout, e:
            print "socket timeout error: %s" %(traceback.format_exc())
        except Exception, e:
            print "socket send and recieve Error: %s" %(traceback.format_exc())
        else:
            cleanup()

    end = time.time()

    print 'eclipse time: %d' %(1000*(end-start))
    #print ret_val