python27 rabbbitmq pikia

vx_guanchaoguo0發表於2024-12-06

需要指定版本

  • pika 0.10.0
# coding=utf-8
import pika

if __name__ == '__main__':
    credentials = pika.PlainCredentials('taskmanager', 'dayuan666')
    connection_parameters = pika.ConnectionParameters(
        '211.159.224.10',
        5672,
        'taskvhost',
        credentials
    )
    try:

        connection = pika.BlockingConnection(connection_parameters)
        channel = connection.channel()
        channel.queue_declare(queue='hello')
        channel.basic_publish(exchange='',routing_key='hello',body='Hello World!')
        print(" [x] Sent 'Hello World!'")
    except pika.exceptions.AMQPConnectionError as e:
        print("連線錯誤: %s".format(e) )
    except pika.exceptions.AMQPChannelError as e:
        print("連線錯誤: %s".format(e))
    except Exception as e:
        print("連線錯誤: %s".format(e))
    finally:
        if 'connection' in locals() and connection.is_open:
            connection.close()

相關文章