如何使用 Python 将 XML 文件发送到 RabbitMQ?
How to send a XML file to RabbitMQ using Python?
我有一个名为 Test.xml
的 xml 文件,我正在尝试使用 python.
发送 RabbitMQ
我知道以下有关 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
如何从 python 完成?
这可以使用 pika 完成,您可以读取文件内容并将其作为大字符串发送到 RabbitMQ。另一方面,您可以使用 ElementTree.fromstring
.
解析内容
连接详细信息:
credentials = pika.PlainCredentials('username', 'password')
conn = pika.BlockingConnection(pika.ConnectionParameters('host', port, 'vhost', credentials))
channel = conn.channel()
出版商:
with open('filename.xml', 'r') as fp:
lines = fp.readlines()
channel.basic_publish('exchange', 'queue', ''.join(lines))
消费者:
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()
希望对您有所帮助!
RabbitMQ 流程:
我有一个名为 Test.xml
的 xml 文件,我正在尝试使用 python.
我知道以下有关 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
如何从 python 完成?
这可以使用 pika 完成,您可以读取文件内容并将其作为大字符串发送到 RabbitMQ。另一方面,您可以使用 ElementTree.fromstring
.
连接详细信息:
credentials = pika.PlainCredentials('username', 'password')
conn = pika.BlockingConnection(pika.ConnectionParameters('host', port, 'vhost', credentials))
channel = conn.channel()
出版商:
with open('filename.xml', 'r') as fp:
lines = fp.readlines()
channel.basic_publish('exchange', 'queue', ''.join(lines))
消费者:
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()
希望对您有所帮助!
RabbitMQ 流程: