从Python发送XML文件到RabbitMQ时获取'位置参数'错误

问题描述 投票:5回答:1

我正在尝试从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))

上面的代码有什么问题?

python-3.x dataframe rabbitmq pika python-pika
1个回答
1
投票

按照@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))
© www.soinside.com 2019 - 2024. All rights reserved.