安装PIP

wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz
tar -xzvf pip-1.5..tar.gz
cd pip-1.5.
python setup.py install 或者
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
python get-pip.py --no-index --find-links=/local/copies https://pip.pypa.io/en/stable/installing/

安装python API: pika

pip install pika

python脚步

#coding:gb2312
import pika
import time
import os
import sys
import time
from stat import S_ISREG, ST_CTIME, ST_MODE try:
import cPickle as pickle
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=
rabbitmq_exchange_name=''
rabbitmq_exchange_routing_key='' class mq_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, , '/', credentials)
connection = pika.BlockingConnection(parameters)
self._connection = connection
self._channel_1 = self._connection.channel()
self._channel_2 = self._connection.channel()
return True
except:
raise
return False 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)
return True
except:
raise
return False def close(self):
self._connection.close()
self._connection = None def sync_file(file_path, from_line):
mq = mq_tool()
if not mq.connect():
return
print ('sync file [%s] from line(%d) start..' %(file_path, from_line))
f = open(file_path, 'r')
line_num =
done_num =
time_s = int(time.time())
for line in f:
line_num +=
if line_num < from_line:
continue
if line.find('>: LogText:') <= :
done_num +=
continue
try: line = line[:-]
ret = True ret = mq.publish(line[:-])
except:
ret = False
raise
if not ret:
break
time_e = int(time.time())
if time_e > time_s:
print done_num
time_s = time_e
done_num +=
print ('sync file [%s] done line(%d)s.' %(file_path, from_line + done_num))
#mq.close()
return done_num sync_file('2017_12_16_00_00_03.log', )

python连接RabbitMQ的更多相关文章

  1. RabbitMQ的安装和使用Python连接RabbitMQ

    绪论 这里的环境使用的是Mac OS X系统,所有的配置和使用都是基于Mac OS X 和Python 2.7 以及对应的pika库的. RabbitMQ的安装和配置 安装部分 #brew insta ...

  2. Python与RabbitMQ交互

    RabbitMQ 消息队列 成熟的中间件RabbitMQ.ZeroMQ.ActiveMQ等等 RabbitMQ使用erlang语言开发,使用RabbitMQ前要安装erlang语言 RabbitMQ允 ...

  3. Python操作RabbitMQ

    RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,RabbitMQ是一个消息代理,从“生产者”接收消息并传递消 ...

  4. Python操作rabbitmq 实践笔记

    发布/订阅  系统 1.基本用法 生产者 import pika import sys username = 'wt' #指定远程rabbitmq的用户名密码 pwd = ' user_pwd = p ...

  5. Python之RabbitMQ操作

    RabbitMQ是一个消息代理,从“生产者”接收消息并传递消息至“消费者”,期间可根据规则路由.缓存.持久化消息.“生产者”也即message发送者以下简称P,相对应的“消费者”乃message接收者 ...

  6. 十一天 python操作rabbitmq、redis

    1.启动rabbimq.mysql 在""运行""里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd, ...

  7. Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy   Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用 ...

  8. python - 操作RabbitMQ

    python - 操作RabbitMQ     介绍 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.MQ全称为Mess ...

  9. 文成小盆友python-num12 Redis发布与订阅补充,python操作rabbitMQ

    本篇主要内容: redis发布与订阅补充 python操作rabbitMQ 一,redis 发布与订阅补充 如下一个简单的监控模型,通过这个模式所有的收听者都能收听到一份数据. 用代码来实现一个red ...

随机推荐

  1. Linux 下的三种时间介绍

    Linux 下的三种时间介绍: Access Time:简写为atime,表示文件访问的时间,当文件内容被访问时,更新atime时间 Modify Time:简写为mtime,表示文件内容修改的时间, ...

  2. UML图类,接口之间的关系

    UML图类之间的关系(来自大话设计模式中的一张图,代表了类,接口之间的各种关系)

  3. Python初学注意问题

    本文是看到的,然后结合自己之前学习的过程也碰到的问题,所以就引用过来了:http://www.cnblogs.com/walkingp/p/3261663.html 一.注意你的Python版本 Py ...

  4. 安装phantomjs(Ubuntu版本 MacOS版本)

    安装phantomjs(Ubuntu版本) 首先:apt-get update # 更新软件列表. apt-get upgrade # 更新软件. 其次再执行如下步骤 步骤一下载包:wget http ...

  5. 未找到路径“/Agent/SissQrTemplate/AddN”的控制器或该控制器未实现 IController。

    未找到路径“/Agent/SissQrTemplate/AddN”的控制器或该控制器未实现 IController. Controller 命名空间错误,应该是Areas.Agent.Controll ...

  6. 黄聪:TortoiseGit(乌龟git)保存用户名密码的方法

    1.在项目文件夹右键--tortoiseGit--设置 2.编辑全局.git/config 3.加上这行代码 里面会有你先前配好的name 和email,只需在下面加一行 [credential] h ...

  7. DEVC怎么建工程

    1.DEVC建工程 1.1 新建项目 打开文件,选择新建-->项目-->ConsoleApplication(控制台程序),输入项目名,选择保存路径.(单独建一个文件夹存放) 项目建成功后 ...

  8. synchronized基础

    synchronized 例子 例1,没有同步的时候运行同一个对象的同一个方法的结果: public class TestSyn { public void showMsg() { try { for ...

  9. github简单命令

    1.安装yum install -y git 2.配置帐户(github.com注册)git config --global user.name goozgkgit config --global u ...

  10. [转][C#]BarCodeToHTML

    /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:http://www.sufeinet.c ...