首先我想说的是,我不仅对 Python 很陌生,而且对一般编程也很陌生。考虑到这一点,这是我的问题。我正在尝试将一些数据发送到我的 Rabbitmq 服务器。下面是我的 Python 代码。 json_data 只是一个保存一些 json 格式数据的变量。
with Connection("amqp://username:password@test_server.local:5672/#/") as conn:
channel = conn.channel()
producer = Producer(channel, exchange = "test_exchange", serializer="json")
producer.publish(json_data)
print "Message sent"
这会产生以下错误:
Traceback (most recent call last): File "test.py", line 43, in <module> producer = Producer(channel, exchange = "test_exchange", serializer="json") File "/Library/Python/2.7/site-packages/kombu/messaging.py", line 83, in __init__ self.revive(self._channel) File "/Library/Python/2.7/site-packages/kombu/messaging.py", line 210, in revive self.exchange = self.exchange(channel) TypeError: 'str' object is not callable
如有任何帮助,我们将不胜感激。谢谢。
请您参考如下方法:
在我的头撞到我的 table 后,我发现 Python 不喜欢交换的调用。然后我的同事告诉我,如果我在发布者中包含交换和路由 key ,它可能会更好。所以现在我的新代码看起来像这样。
# Creates the exchange and queue and binds them
# If already created on the server side these steps are not needed
exchange = Exchange("test_exchange", "direct", durable=True)
queue = Queue("test_q", exchange = exchange, routing_key = "test")
# The last foreword slash is the virtual host
with Connection("amqp://username:password@hostname:5672/", "/") as conn:
channel = conn.channel()
producer = Producer(channel, exchange = exchange, serializer="json")
for key, value in json_data.items():
producer.publish(exchange = exchange, routing_key = "test", body = {key:value})
print "Message sent!"
