#coding:gb2312
import pika
import time
import os
import sys
import timefromstat import S_ISREG, ST_CTIME, ST_MODEtry:
import cPickleaspickle
except ImportError:
import pickle
cwd=os.path.dirname(os.path.realpath(__file__))
os.chdir(cwd)
rabbitmq_username=''rabbitmq_password=''rabbitmq_host='mq..com'rabbitmq_port=5675rabbitmq_exchange_name=''rabbitmq_exchange_routing_key=''
classmq_tool:
def __int__(self):
self._connection=None
self._channel_1=None
self._channel_2=None
def connect(self):try:
credentials=pika.PlainCredentials(rabbitmq_username, rabbitmq_password)
parameters= pika.ConnectionParameters(rabbitmq_host, 5675, '/', credentials)
connection=pika.BlockingConnection(parameters)
self._connection=connection
self._channel_1=self._connection.channel()
self._channel_2=self._connection.channel()returnTrue
except:
raisereturnFalse
def publish(self, txt):try:
# channel=self._connection.channel()
self._channel_1.basic_publish(exchange=rabbitmq_exchange_name,
routing_key=rabbitmq_exchange_routing_key,
body=txt)returnTrue
except:
raisereturnFalse
def close(self):
self._connection.close()
self._connection=None
def sync_file(file_path, from_line):
mq=mq_tool()ifnot mq.connect():return 0print ('sync file [%s] from line(%d) start..' %(file_path, from_line))
f= open(file_path, 'r')
line_num= 0done_num= 0time_s= int(time.time())for line inf:
line_num+= 1
if line_num
if line.find('>: LogText:') <= 0:
done_num+= 1
continue
try:
line= line[47:-1]
ret=True
ret= mq.publish(line[47:-1])
except:
ret=False
raiseifnot ret:breaktime_e= int(time.time())if time_e >time_s:
print done_num
time_s=time_e
done_num+= 1print ('sync file [%s] done line(%d)s.' %(file_path, from_line +done_num))
#mq.close()returndone_num
sync_file('2017_12_16_00_00_03.log', 0)