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)
  • 相关阅读:
    HDU 4472 Count DP题
    HDU 1878 欧拉回路 图论
    CSUST 1503 ZZ买衣服
    HDU 2085 核反应堆
    HDU 1029 Ignatius and the Princess IV
    UVa 11462 Age Sort
    UVa 11384
    UVa 11210
    LA 3401
    解决学一会儿累了的问题
  • 原文地址:https://www.cnblogs.com/linn/p/8063955.html
Copyright © 2011-2022 走看看