1

I am having an xml file called Test.xml which I am trying to send RabbitMQ using python.

I know below deatails regarding the Rabbit MQ

Hostname: xxx.xxxx.xxx

AMQP Port (SSL)  :4589

ESB Portal (Message Search): http://xxx.xxx.xxx:8585

RabbitMQ Web UI (https) :https://xxx.xxx.xxxx:15672 

How can this be done from python?

aeapen
  • 871
  • 1
  • 14
  • 28
  • Have you tried anything yet? Their documentation is pretty good. What issues are you having ? – theMayer Feb 17 '20 at 06:28
  • @theMayer, I am also trying to do the same. I have notcoema cross anyproper document that helps to me send file to rabbit MQ. If you could post this as anser as would be really helpfull all reades – LDF_VARUM_ELLAM_SHERIAAVUM Feb 17 '20 at 06:32

1 Answers1

3

This can be done using pika, you can read the file content and send it as a big string to RabbitMQ. And on the other side you can parse the content using ElementTree.fromstring.

Connection details:

credentials = pika.PlainCredentials('username', 'password')
conn = pika.BlockingConnection(pika.ConnectionParameters('host', port, 'vhost', credentials))
channel = conn.channel()

Publisher:

with open('filename.xml', 'r') as fp:
    lines = fp.readlines()
channel.basic_publish('exchange', 'queue', ''.join(lines))

Consumer:

def on_message(unused_channel, unused_method_frame, unused_header_frame, body):
    lines = body.decode()
    doc = ElementTree.fromstring(lines)
    tags = doc.findall("tag")

    ## DO YOUR STUFF HERE

channel.basic_consume('queue', on_message)
channel.start_consuming()

Hope this helps!

RabbitMQ flow:

RabbitMQ flow

Reference: RabbitMQ docs

bumblebee
  • 1,811
  • 12
  • 19
  • 2
    can you telll me what does `exchange`,`queue` in busic_publish means – aeapen Feb 18 '20 at 01:18
  • The `exchange` routes the messages to the `queue`. The `queue` is the name of the `queue` to which the message is to be sent – bumblebee Feb 18 '20 at 02:03
  • 1
    @bumblebee,If my understanding is correct from your answer,If i am sending just xml file to RabbitMQ then `Publisher` is only needed . Also I just need to know the hostname, port , username and password for sending the file – pankaj Feb 18 '20 at 05:03
  • 1
    @pankaj If you just want to send the contents of the XML file to the RabbitMQ queue, then `publisher` alone is enough. But queues are often associated with `consumers` which consume the messages from the queue. In order to publish or consume the message, the RabbitMQ client needs to make a `connection` to the RabbitMQ server/host. Hence you need the hostname, port and credentials – bumblebee Feb 18 '20 at 05:15
  • 1
    @bumblebee, Thanks great info. In my case I just need push the xml to the MQ queue. Its an another team is responsible for reading the data from MQ. I guess publisher is only needed – pankaj Feb 18 '20 at 05:22
  • 1
    @pankaj Then publisher is what you need. Glad to help! – bumblebee Feb 18 '20 at 05:28