从 Python 向 RabbitMQ 发送 XML 文件时出现 'positional argument' 错误
Getting 'positional argument' Error while sending a XML file to RabbitMQ from Python
我正在尝试从 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 所建议的,您在 basic.publish
方法中缺少 body
键。此外,basic_publish
方法没有名为 queue
的参数。请看一下它的实现 docs
编辑#1:我已经在别处回答了这个问题
编辑 #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))
我正在尝试从 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 所建议的,您在 basic.publish
方法中缺少 body
键。此外,basic_publish
方法没有名为 queue
的参数。请看一下它的实现 docs
编辑#1:我已经在别处回答了这个问题
编辑 #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))