RABBITMQ Send Message +python

Source: Internet
Author: User

The interface uses two queue listener information and has two test environments, so you need to send test data to MQ:

Python uses the Pika package: Pika is a RabbitMQ (amqp-0-9-1) the client library for Python.

can refer to: Https://github.com/pika/pika

= Connection.channel () channel.basic_publish (exchange=' Examplerouting_key=' Test  Body=' Test Message') connection.close ()         

Encapsulates the sending message into a function: stream.py

The specific code is as follows:

Import Pika
Import JSON
From config import env


def send_msg (Msg_exchange, Msg_key, MSG, msg_type=none):
If str (env.__name__) = = ' Beta ':
If Msg_type is None:
Connection = Pika. Blockingconnection (Pika. Urlparameters(
' Amqp://talaris:[email Protected]:5672/talaris '))
Else
Connection = Pika. Blockingconnection (Pika. Urlparameters (
' Amqp://talaris:[email Protected]:5672/clair '))

Elif str (env.__name__) = = ' Alpha ':
If Msg_type is None:
Connection = Pika. Blockingconnection (Pika. Urlparameters (
' Amqp://talaris:[email Protected]:5672/talaris '))
Else
Connection = Pika. Blockingconnection (Pika. Urlparameters (
' Amqp://talaris:[email Protected]:5672/clair '))

Channel = Connection.channel ()

Channel.basic_publish (Exchange=msg_exchange,
Routing_key=msg_key,
Body=json.dumps (msg))

Print ("[x] Sent%r:%r"% (Msg_key, msg))
Connection.close ()

PS: I use the pika. Urlparameters, you can also use Pika. Connectionparameters (host=' localhost'), refer to GitHub for details

RABBITMQ Send Message +python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.