我正在尝试从python向RabbitMQ发送XML文件,但出现以下错误
错误
File "<ipython-input-134-8a1b7f8b2e41>", line 3
channel.basic_publish(exchange='',queue='abc',''.join(lines))
^
SyntaxError: positional argument follows keyword argument
我的代码
import ssl
!pip install pika
import pika
ssl_options = pika.SSLOptions(ssl._create_unverified_context())
credentials = pika.PlainCredentials(username='abcc', password='abcc')
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='xxxx', port=5671, virtual_host ='xxx', credentials=credentials,
ssl_options=ssl_options))
channel = connection.channel()
result = channel.queue_declare(queue='abc')
with open('20200205280673.xml', 'r') as fp:
lines = fp.readlines()
channel.basic_publish(exchange='',queue='abc',''.join(lines))
上面的代码有什么问题?
按照@ymz的建议,您缺少body
方法中的basic.publish
键。另外,basic_publish
方法没有名为queue
的参数。请查看其实现docs
编辑#1:我已经在其他How to send a XML file to RabbitMQ using Python?]处回答了这个问题
Edit#2:
自动发布XML文件。假设所有文件都存在于名为xml_files
的目录中import os
DIR = '/path/to/xml_files'
for filename in os.listdir(DIR):
filepath = f"{DIR}/{filename}"
with open(filepath) as fp:
lines = fp.readlines()
channel.basic_publish(exchange='exchange', routing_key='queue', body=''.join(lines))