#!/bin/env python from socket import * import Mobigen.Common.Log as Log Log.Init() import sys, time def ConnectEventFlow(IP, PORT): try: sock = socket(AF_INET, SOCK_STREAM) sock.connect((IP, PORT)) return sock except: return Nonde def DisConnectEventFlow(sock): try: sock.sendall("bye\n") except: __LOG__.Exception() time.sleep(1) sock.close() def main(): __LOG__.Trace("Process Start...") if len(sys.argv) != 3: print '%s IP PORT' % sys.argv[0]
#!/usr/bin/env python # -*- coding: utf-8 -*- import datetime import os import sys import signal import ConfigParser import glob import json import Mobigen.Common.Log as Log Log.Init() import shutil import subprocess import SelectToIris import socket from apscheduler.schedulers.blocking import BlockingScheduler # Process Shutdown Flag SHUTDOWN = False NETSTAT_LOCAL_ADDR_IDX = 3 NETSTAT_FOREIGN_ADDR_IDX = 4 NETSTAT_STATE_IDX = 5 MQ_PORT = 5671 # @param signum # @param frame -
eqs.setDaemon(True) eqs.start() server = EventQueueDist( eqcList, RecvType=socketType, RecvPort=recvPort, Mode=mode ) server.setDaemon(True) server.start() while 1 : ### need to hand signal ### time.sleep(1) for eqc in eqcList : eqc.shutdown = True eqs.shutdown = True server.shutdown = True time.sleep(3) except : __LOG__.Exception() __LOG__.Trace( '--- main end' ) if __name__ == '__main__' : import Mobigen.Common.Log as Log; import sys, os, getopt; OPT, ARGS = getopt.getopt(sys.argv[1:], 'dfbp') try : LOG_NAME = '~/log/%s.%s.log' % (os.path.basename(sys.argv[0]), ARGS[0]) except : LOG_NAME = '~/log/%s.log' % (os.path.basename(sys.argv[0])) try : OPT.index(('-d', '')); Log.Init() except : Log.Init(Log.CRotatingLog(os.path.expanduser(LOG_NAME), 1000000, 10)) #sys.argv = [sys.argv[0]] + ARGS main()
# -*- coding: cp949 -*- try : import psyco; psyco.full() except : pass import os import sys import time import getopt import select import threading from socket import * from telnetlib import Telnet import Mobigen.DataProcessing.CSV as CSV import Mobigen.Common.Log as Log; Log.Init() import signal; SHUTDOWN = False def handler(sigNum, frame) : global SHUTDOWN; SHUTDOWN = True signal.signal(signal.SIGTERM, handler) signal.signal(signal.SIGINT, handler) class Recv(threading.Thread) : def __init__(self, ip, id, passwd, rmtDir, recvIp, recvPort, localDir, **args) : threading.Thread.__init__(self) self.rmtIp = ip self.rmtId = id self.rmtPasswd = passwd self.rmtDir = rmtDir self.ip = recvIp
#!/usr/bin/python #--------------------------------------------------------------------- # version | date : writer : description #---------+----------------------------------------------------------- # V2.0 | 070423 : cdssw : final #--------------------------------------------------------------------- import Mobigen.Common.Log as Log; Log.Init() import Mobigen.SF.lib.SFProtocol as sfp import os, sys, getopt from socket import * import signal SHUTDOWN = False def shutdown(sigNum=0, frame=0) : global SHUTDOWN SHUTDOWN = True signal.signal(signal.SIGTERM, shutdown) signal.signal(signal.SIGINT, shutdown) signal.signal(signal.SIGHUP, shutdown) def main() : global SHUTDOWN try : optStr = sfp.getOptStr() optList, args = getopt.getopt(sys.argv[1:], optStr+'t:')
#!/usr/bin/python #--------------------------------------------------------------------- # version | date : writer : description #---------+----------------------------------------------------------- # V2.0 | 070423 : cdssw : final #--------------------------------------------------------------------- import Mobigen.Common.Log as Log Log.Init() import Mobigen.Common.SFProtocol.SFProtocol as sfp import os, sys, getopt from socket import * def main(): try: optStr = sfp.getOptStr() optList, args = getopt.getopt(sys.argv[1:], optStr + 't:') if len(args) != 2: raise Exception opts = {} for optKey, optVal in optList: opts[optKey] = optVal except: print 'usage : %s [-options] servIp servPort' % (sys.argv[0]) sfp.prnOpt() sys.exit() sf = sfp.SFProtocol(sfp.SFP_CH_T, **opts)
if opts.has_key('--output-file'): fileName = opts['--output-file'].strip() g_OUT_FILE = open(fileName, 'w') except: print "ERROR : Check output-file=: ", fileName, sys.exc_info() time.sleep(10) sys.exit() #------------------------------------------------------- # SendMessage Timeout #------------------------------------------------------- try: if opts.has_key('-t'): g_SEND_TIMEOUT = int(opts['-t'].strip()) except: pass # default timeout #------------------------------------------------------- # HeartBeat, Key #------------------------------------------------------- g_INIT_KEY = '0,*,*' # heartbeat,key1,key2 g_INIT_KEY = args[2] LOG_FILE = '%s.log' % sys.argv[0] Log.Init(Log.CRotatingLog(LOG_FILE, 10000000, 3, 'a', debugLevel)) __LOG__.Trace( "START :LEVEL [%s:%d], KEY:[%s]+++++++++++++++++++++++++++++++++++++++++++++++++" % (strLevel, debugLevel, g_INIT_KEY), (logging.CRITICAL)) main()
#!/usr/bin/python #--------------------------------------------------------------------- # version | date : writer : description #---------+----------------------------------------------------------- # V2.0 | 070423 : cdssw : final #--------------------------------------------------------------------- import Mobigen.Common.Log as Log Log.Init() import Mobigen.Common.SFProtocol.SFProtocol as sfp import os, sys, getopt from socket import * import select import signal SHUTDOWN = False ##### psyco ##### try: import psyco psyco.full() except ImportError: pass ################# def shutdown(sigNum=0, frame=0): global SHUTDOWN SHUTDOWN = True
def main(): global SHUTDOWN try: ### 뒤에 인수가 올경우 : 붙이고, 아닐경우 안붙인다 ### : 에 상관없이 optList 는 튜플의 리스트로 반환된다. # optList, args = getopt.getopt(sys.argv[1:], 'p:f:t:w:n:i:') optList, args = getopt.getopt(sys.argv[1:], 'p:m:l:') if len(args) != 2: raise Exception optDict = {} for optKey, optVal in optList: optDict[optKey] = optVal except: #print 'usage : %s [-pftwni] homeDir infoFileName' % (sys.argv[0]) #print ' -p[11] : protocol type, default:1' #print ' -f[okd|kd|d] : okd : message format = option(16byte) key(10byte) data' #print ' kd : message format = key(10byte) data' #print ' d : message format = data' #print ' : default format = fileTime(yyyymmddhhmmss) option(16byte) key(10byte) data' #print ' -t[Num] : read block timeout second, default is 1, for signal process' #print ' -w[Num] : if no date to read, wait this second, default is 0.1' #print ' -n[Num] : if no data to read, list up next file for this second interval, default is 10' #print ' -i[Num] : info file update period record count, default is 10' #sys.exit() print 'VERSION: ' + VERSION print 'usage : %s [-p] serviceIP servicePort' % (sys.argv[0]) print ' -p[1] : protocol type, default:6' print ' -l /home/eva/log/dict.log : logfile full path ' print ' -m[bufsize] : socket read buffer size, only use in pType >= 6, default = 0' sys.exit() ### Env ### dict = {} servIP = args[0] servPort = int(args[1]) qryCnt = 0 logPrnCnt = 0 defaultPType = 6 if '-p' in optDict: defaultPType = int(optDict['-p']) if '-l' in optDict: Log.Init(Log.CRotatingLog(optDict['-l'], 10000000, 2)) makeFileBufSize = 0 if '-m' in optDict: makeFileBufSize = int(optDict['-m']) ### Socket Create ### server = mkServer(servIP, servPort) sockList = [server] childPType = {} childAddr = {} childSockFD = {} global SHUTDOWN global DEBUG while SHUTDOWN == False: try: inputReady, outputReady, exceptReady = select.select( sockList, [], [], 1) except Exception, err: __LOG__.Exception() break if len(inputReady) == 0: logPrnCnt += 1 if logPrnCnt % 10 == 0: __LOG__.Trace("[%s:%s] ready, connections=[%s], qryCnt=[%s]" % (servIP, servPort, len(sockList), qryCnt)) continue for sock in inputReady: qryCnt += 1 if qryCnt % 10000 == 0: __LOG__.Trace("[%s:%s] ready, connections=[%s], qryCnt=[%s]" % (servIP, servPort, len(sockList), qryCnt)) if sock == server: childSock, addr = server.accept() childSock.settimeout(1) # no use for select sockList.append(childSock) addr = str(addr) childAddr[childSock] = addr childPType[childSock] = defaultPType childSockFD[childSock] = childSock.makefile( bufsize=makeFileBufSize) __LOG__.Trace('Connected : %s' % addr) else: try: res = False try: res = actClient(sock, childSockFD[sock], dict, childAddr[sock], childPType) except: res = False if res == False: sockList.remove(sock) sock.close() __LOG__.Trace('Removed : %s ' % childAddr[sock]) del (childPType[sock]) del (childAddr[sock]) del (childSockFD[sock]) elif type(res) == int: server.close() sockList.remove(server) server = mkServer(servIP, res) sockList.append(server) servPort = res except Exception, err: __LOG__.Exception() continue
def SetLog(self): Log_file = os.path.join(self.LOG_PATH, "%s.log" % os.path.basename(sys.argv[0])[:-3]) Log.Init( Log.CRotatingLog(os.path.expanduser(Log_file), self.LOG_SIZE, self.LOG_COUNT))
def main(): global SHUTDOWN try: optStr = sfp.getOptStr() optStr += "f:" optList, args = getopt.getopt(sys.argv[1:], optStr, [ "deny=", ]) if len(args) != 2: raise Exception opts = {} for optKey, optVal in optList: opts[optKey] = optVal except: print 'VERSION: %s' % VERSION print 'usage : %s [-options] servIP servPort' % (sys.argv[0]) sfp.prnOpt() print " -f[logfilePath] : logfilePath" print " --deny=host.deny : host deny ip" sys.exit() key1Hash = {} key2Hash = {} keyBothHash = {'*,*': []} if opts.has_key("-f"): # -f logfilePath fileName = opts["-f"] Log.Init(Log.CRotatingLog(fileName, 10000000, 3)) else: # opts Log.Init(Log.CStandardErrorLog()) pType = 1 # default protocol type 1 if opts.has_key("-p"): try: pType = int(opts["-p"]) except: pType = 1 denyIpHash = {} if opts.has_key("--deny"): fileName = opts["--deny"] readDenyFile(fileName, denyIpHash) servIP = args[0] servPort = int(args[1]) servsock = socket(AF_INET, SOCK_STREAM) servsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) servsock.bind(('', int(servPort))) servsock.listen(10) #servsock.setblocking(False) # non block global inSockList, sockConnectorHash inSockList = [servsock] sockConnectorHash = {} conList = [] __LOG__.Trace("[%s:%s] Start ---" % (servIP, servPort)) startSec = time.time() qryCnt = 1 while SHUTDOWN == False: try: #{ try: inputReady,outputReady,exceptReady = \ select.select(inSockList,[],[], 0.1) except Exception, err: __LOG__.Exception() raise sfp.SFPDiscon, err if len(inputReady) == 0: aliveSec = time.time() - startSec if aliveSec >= 60: __LOG__.Trace( "[%s:%s] ready, conList=[%s], qryCnt=[%s]" \ % (servIP, servPort, len(conList), qryCnt) ) startSec = time.time() for th in conList: if th.heartBeatSec != 0: th.ansHeart() if qryCnt % 100 == 0: for th in conList: if th.heartBeatSec != 0: th.ansHeart() if qryCnt % 10000 == 0: __LOG__.Trace( "[%s:%s] ready, conList=[%s], qryCnt=[%s]" \ % (servIP, servPort, len(conList), qryCnt) ) idx = 0 for sock in inputReady: #{ idx += 1 __LOG__.Trace("inputReady, [%d/%d]" % (idx, len(inputReady))) qryCnt += 1 if sock == servsock: cSock, addr = servsock.accept() # 접속 거부 if denyIpHash.has_key(addr[0]): cSock.close() __LOG__.Trace("deny ip [%s]" % addr[0]) continue inSockList.append(cSock) addr = str(addr) connector = Connector(cSock, addr, key1Hash, key2Hash, keyBothHash, conList, **opts) sockConnectorHash[cSock] = connector __LOG__.Trace("connected, addr=%s" % addr) else: connector = sockConnectorHash[sock] try: # try idx = 0 readData(sock, key1Hash, key2Hash, keyBothHash) while pType == 6: if (not readData(sock, key1Hash, key2Hash, keyBothHash, True)): __LOG__.Trace( "+++++ readData END TIMEOUT ++++++ %s,fd:[%d]" % (addr, sock.fileno())) break idx += 1 if idx >= 100: __LOG__.Trace( "+++++ readData END 100 ++++++ %s,fd:[%d]" % (addr, sock.fileno())) break except Exception, err: __LOG__.Exception() try: inSockList.remove(sock) except: pass try: del (sockConnectorHash[sock]) except: pass connector.deregister() sock.close() continue