zoukankan      html  css  js  c++  java
  • python连接RabbitMQ

    安装PIP

    wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz
    tar -xzvf pip-1.5.4.tar.gz
    cd pip-1.5.4
    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=5675
    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, 5675, '/', 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 0
        print ('sync file [%s] from line(%d) start..' %(file_path, from_line))
        f = open(file_path, 'r')
        line_num = 0
        done_num = 0
        time_s = int(time.time())
        for line in f:
            line_num += 1
            if line_num < from_line:
                continue
            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
                raise
            if not ret:
                break
            time_e = int(time.time())
            if time_e > time_s:
                print done_num
                time_s = time_e
            done_num += 1
        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', 0)
  • 相关阅读:
    记一次oracle新建用户及分配指定表权限的操作记录
    [转]word中不显示mathtype公式,只显示方框,双击后可以再mathtype里面看到公式
    C、C++成员变量对齐
    include头文件:C++标准库与C标准库
    [转]本专业部分国际会议及刊物影响因子排名
    使用Winbase.h
    [转]printf 字符串格式化
    1.6.2如何使用位逻辑运算(例如与、或、位移)来实现位向量?
    文章中图表自动编号
    取样问题 总数n事先不知道,等概率取样 (编程珠玑chapter12 课后题10)
  • 原文地址:https://www.cnblogs.com/linn/p/8063955.html
Copyright © 2011-2022 走看看