def doSocket(connection,addr): ''' @connection: socket连接 ''' try: i=0 j=0 data = recvs = '' while True: data = connection.recv(SOCKET['BUFSIZE']) if data == '': i=i+1 recvs = recvs + data if data.endswith('EOF') or recvs == 'PORT_IS_ALIVE' or i > 2 or j > 10: break time.sleep(round(float(random.randrange(0, 10000, 1))/10000,4)) j = j + 1 #recvs = connection.recv(SOCKET['BUFSIZE']) ip=addr[0] if recvs == 'PORT_IS_ALIVE': save_log('INFO', recvs) connection.close() sys.exit() if not recvs or recvs == '': connection.close() sys.exit() try: cmd = decode(recvs) except Exception as e: save_log('ERROR', 'decode error:'+str(e)+',recvs :'+str(recvs)) connection.close() sys.exit() if cmd == 'CMD_STAT': save_log('INFO', cmd) connection.send(encode('CMD_STAT_OK')) connection.close() sys.exit() if cmd.endswith('check'): save_log('CHECK', cmd) connection.send(encode('error')) connection.close() sys.exit() # is myself def cname = isMySelfCmd(cmd) #if batch cmd batch = isBatchCmd(ayCmd(cmd)) if cname: try: infos = OsaChooseDef(defname=cname,ip = ip,fromPhpCmd=cmd) except Exception,e: connection.close() save_log('ERROR', str(e)) #处理批量操作分支 elif batch: connection.close() rinfo = chooseBatchDef(ayCmd(cmd),cmd) save_log('INFO',str(ayCmd(cmd))) ##处理批量操作结果 ret = "{'batchinfo':"+str(cmd)+",'command':'batchresult','batchresult':"+str(rinfo)+"}" port=SOCKET['AGENTPORT'] r = RproSocket(ip, port, ret) if r: sys.exit() else: time.sleep(0.3) r = RproSocket(ip, port, str(ret), type=None) sys.exit()
rinfo = chooseBatchDef(ayCmd(cmd),cmd) save_log('INFO',str(ayCmd(cmd))) ##处理批量操作结果 ret = "{'batchinfo':"+str(cmd)+",'command':'batchresult','batchresult':"+str(rinfo)+"}" port=SOCKET['AGENTPORT'] r = RproSocket(ip, port, ret) if r: sys.exit() else: time.sleep(0.3) r = RproSocket(ip, port, str(ret), type=None) sys.exit() else: infos = execCMD(cmd) save_log('INFO', infos[0]) ldata=len(encode(infos[1])) toAngentData=encode(infos[1]) try: connection.send(toAngentData) except Exception as e: connection.send(infos[1]) sys.exit() connection.close() sys.exit() except Exception as e: save_log('ERROR', 'python recv error:'+str(e)) connection.close() sys.exit()