How to send a XML file to RabbitMQ using Python?

馋奶兔 提交于 2020-06-27 04:22:32

问题


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?


回答1:


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:

Reference: RabbitMQ docs



来源:https://stackoverflow.com/questions/60256957/how-to-send-a-xml-file-to-rabbitmq-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!