def recv_xls(): idx = gConfig['http_query_proxy'].rindex(':') port = gConfig['http_query_proxy'][idx+1:] context = zmq.Context() recvier = context.socket(zmq.REP) recvier.setsockopt(zmq.LINGER, 1) recvier.bind("tcp://*:%s" % port) cnt = 0 while 1: data = recvier.recv() if len(data)>0: #data = dec1(data) print(data) s = '' try: obj = {} try: obj = json.loads(data) except UnicodeDecodeError: try: obj = json.loads(dec1(data)) except: raise jsonobj = {} name = None op = None id = None position = None for k in obj.keys(): if k in ['name']: name = obj[k] elif k in ['id']: id = obj[k] elif k in ['position']: position = obj[k] elif k in ['op']: op = obj[k] if name and op and id: s = query(name, op, id) elif name and op: s = query(name, op) elif name and position and id: s = query_position(name, position, id) except: s = json.dumps({'error':u'格式错误'}, ensure_ascii=True, encoding=ENCODING1) recvier.send(s) gevent.sleep(0.1) recvier.close() context.term()
def query_http_gevent(url, method='GET', data={}): s = '' request = None response = None try: urlobj = None if method=='GET': urlobj = URL(url + '?' + urllib.urlencode(data)) if method=='POST': urlobj = URL(url) http = HTTPClient.from_url(urlobj) header = {"Content-Type": 'text/json;charset=' + ENCODING} if method=='GET': response = http.get(urlobj.request_uri, header) elif method=='POST': response = http.post(urlobj.request_uri, urllib.urlencode(data), header) if response and response.status_code == 200: s = response.read() #n = y.next() #while n: #s += n #try: #n = y.next() #except StopIteration: #break #y.release() else: s = u'访问服务器出错' #else: #s = '''{"error":"Illegal request"}''' except: m = sys.exc_info()[1].message if len(m) == 0: if hasattr(sys.exc_info()[1], 'strerror') and hasattr(sys.exc_info()[1], 'errno'): m = u'%s(%s)' % (dec1(sys.exc_info()[1].strerror), sys.exc_info()[1].errno) s = '''{"error":"%s"}''' % m finally: if response: response.release() return s