python连接RabbitMQ
安装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的更多相关文章
- RabbitMQ的安装和使用Python连接RabbitMQ
绪论 这里的环境使用的是Mac OS X系统,所有的配置和使用都是基于Mac OS X 和Python 2.7 以及对应的pika库的. RabbitMQ的安装和配置 安装部分 #brew insta ...
- Python与RabbitMQ交互
RabbitMQ 消息队列 成熟的中间件RabbitMQ.ZeroMQ.ActiveMQ等等 RabbitMQ使用erlang语言开发,使用RabbitMQ前要安装erlang语言 RabbitMQ允 ...
- Python操作RabbitMQ
RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,RabbitMQ是一个消息代理,从“生产者”接收消息并传递消 ...
- Python操作rabbitmq 实践笔记
发布/订阅 系统 1.基本用法 生产者 import pika import sys username = 'wt' #指定远程rabbitmq的用户名密码 pwd = ' user_pwd = p ...
- Python之RabbitMQ操作
RabbitMQ是一个消息代理,从“生产者”接收消息并传递消息至“消费者”,期间可根据规则路由.缓存.持久化消息.“生产者”也即message发送者以下简称P,相对应的“消费者”乃message接收者 ...
- 十一天 python操作rabbitmq、redis
1.启动rabbimq.mysql 在""运行""里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd, ...
- Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用 ...
- python - 操作RabbitMQ
python - 操作RabbitMQ 介绍 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.MQ全称为Mess ...
- 文成小盆友python-num12 Redis发布与订阅补充,python操作rabbitMQ
本篇主要内容: redis发布与订阅补充 python操作rabbitMQ 一,redis 发布与订阅补充 如下一个简单的监控模型,通过这个模式所有的收听者都能收听到一份数据. 用代码来实现一个red ...
随机推荐
- MYSQL之 GroupCommit
组提交(group commit)是MYSQL处理日志的一种优化方式,主要为了解决写日志时频繁刷磁盘的问题.组提交伴随着MYSQL的发展不断优化,从最初只支持redo log 组提交,到目前5.6官方 ...
- maven 内置变量
${basedir} 项目根目录 ${project.build.directory} 构建目录,缺省为target ${project.build.outputDirectory} 构建过程输出目录 ...
- SmartGit 授权Non-Commerical
Window: %APPDATA%/syntevo/SmartGit/ 搜索settings.xml, 并且移除这个文件 重启
- Keras/Tensorflow训练逻辑研究
Keras是什么,以及相关的基础知识,这里就不做详细介绍,请参考Keras学习站点http://keras-cn.readthedocs.io/en/latest/ Tensorflow作为backe ...
- ML: 聚类算法R包 - 模型聚类
模型聚类 mclust::Mclust RWeka::Cobweb mclust::Mclust EM算法也称为期望最大化算法,在是使用该算法聚类时,将数据集看作一个有隐形变量的概率模型,并实现模型最 ...
- 使用Tesseract-OCR 进行文字识别
关于中文的识别,效果比较好而且开源的应该就是Tesseract-OCR了,所以自己亲身试用一下,分享到博客让有同样兴趣的人少走弯路. 文中所用到的身份证图片资源是百度找的,如有侵权可联系我删除. 一. ...
- linux du 查看文件及文件夹大小
1.查看当前目录大小: 如在:/home/jzw/share/ du -sh 1.6G . 2.查看当前目录下各个文件夹的大小: 如在:/home/jzw/share/ du -sh * 3.6 ...
- how to install an older version of package via NuGet?
转载 http://stackoverflow.com/questions/10206090/how-to-install-an-older-version-of-package-via-nuget ...
- CI 数据库操作总结
最简单示例 $query = $this->db->query("YOUR QUERY"); foreach ($query->result() as $row) ...
- windows下安装mingw-w64
mingw-w64应该可以算是mingw的改进版本吧,mingw系列编译器是非常好的并且主流的c/c++编译器 mingw-w64只负责程序的编译,只提供命令行操作没有编辑代码的图像界面,代码的编写需 ...