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 ...
随机推荐
- php获取指定文件夹中文件名称
/** * php获取指定文件夹中文件名称 * @author jackie <2018.10.10> */ public static function getFileName($fil ...
- MySQL通过Navicat实现远程连接
直接使用Navicat通过IP连接会报各种错误,例如:Error 1130: Host '192.168.1.80' is not allowed to connect to this MySQL s ...
- 五、springboot(二)配置数据源oracle
1.添加依赖 <!-- jpa --> <dependency> <groupId>org.springframework.boot</groupId> ...
- Spring4相关jar包介绍(转)
Spring4相关jar包介绍 spring-core.jar(必须):这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当 ...
- redis 如何查看所有的key
可以使用KEYS 命令 KEYS pattern 例如, 列出所有的key redis> keys * 列出匹配的key redis>keys apple* 1) apple1 2) ap ...
- python json.dumps(output) ^ SyntaxError: invalid syntax
问题 下面代码在有些机器上执行正常,有些机器上执行报错: import json output={} print json.dumps(output) python代码报错: line 277 pri ...
- 使用vivado将bit文件转化为mcs文件
使用vivado将bit文件转化为mcs文件 1.在Tcl Console中运行脚本: write_cfgmem -force -format MCS -size 64 -interface spix ...
- python 中变量引用问题
普通变量,如a=10,str="fdaf",它们在函数内的值是不会被带到函数外的,除非在函数内加上global,而引用是惰性原则,从最近的同名父级同名变量引用值 其它变量如列表,字 ...
- Linux ssh服务器配置
配置文件在/etc/sshd_config,注意只有root可rw,其他所有用户权限为---. 配置说明可参考man sshd_config. 如果更改了服务器端口号,并且启用了SELinux,需要执 ...
- C++中sort函数小结
我们都知道,sort函数是C++标准库<algorithm>中的一个库函数.它的功能是对数组/容器中的元素进行排序.用法示例如下: 一.对数组进行排序 示例: int a[] = {1,3 ...