python实时连接rabbitmq_python连接RabbitMQ

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值