【问题标题】:Could not connect to RabbitMQ from Python无法从 Python 连接到 RabbitMQ
【发布时间】:2017-03-01 10:38:35
【问题描述】:

我正在学习 Python,但遇到了奇怪的问题。我在我的 macOS Sierra 本地机器上运行 RabitMQ。从 python 脚本连接到 RabbitMQ 服务器总是失败并出现套接字超时错误。同时我可以通过 telnet 连接。 运行 Python 2.7 和库 kombu:

# test.py
from kombu import Connection

connection = Connection('amqp://127.0.0.1')
connection.connect()

结果:

Thunderstorm:test max$ python test.py
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    connection.connect()
  File "/Library/Python/2.7/site-packages/kombu/connection.py", line 261, in connect
    return self.connection
  File "/Library/Python/2.7/site-packages/kombu/connection.py", line 802, in connection
    self._connection = self._establish_connection()
  File "/Library/Python/2.7/site-packages/kombu/connection.py", line 757, in _establish_connection
    conn = self.transport.establish_connection()
  File "/Library/Python/2.7/site-packages/kombu/transport/pyamqp.py", line 130, in establish_connection
    conn.connect()
  File "/Library/Python/2.7/site-packages/amqp/connection.py", line 300, in connect
    self.drain_events(timeout=self.connect_timeout)
  File "/Library/Python/2.7/site-packages/amqp/connection.py", line 464, in drain_events
    return self.blocking_read(timeout)
  File "/Library/Python/2.7/site-packages/amqp/connection.py", line 468, in blocking_read
    frame = self.transport.read_frame()
  File "/Library/Python/2.7/site-packages/amqp/transport.py", line 237, in read_frame
    frame_header = read(7, True)
  File "/Library/Python/2.7/site-packages/amqp/transport.py", line 377, in _read
    s = recv(n - len(rbuf))
socket.timeout: timed out

需要几秒钟并返回超时。此时我在 RabbitMq 日志中看到:

=INFO REPORT==== 1-Mar-2017::12:25:00 ===
accepting AMQP connection <0.23773.4> (127.0.0.1:60600 -> 127.0.0.1:5672)

=WARNING REPORT==== 1-Mar-2017::12:25:08 ===
closing AMQP connection <0.23773.4> (127.0.0.1:60600 -> 127.0.0.1:5672):
client unexpectedly closed TCP connection

防火墙已关闭。 Telnet 连接成功:

Thunderstorm:test max$ telnet 127.0.0.1 5672
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

如何调试这个问题?

【问题讨论】:

  • 您是否尝试过像在 telnet 中一样将端口号放入连接中?即Connection('amqp://127.0.0.1:5672')
  • 是的,我使用端口,使用默认凭据,但总是得到相同的结果。 Connection('amqp://127.0.0.1:5672')Connection('amqp://guest:guest@127.0.0.1:5672')

标签: python python-2.7 rabbitmq amqp kombu


【解决方案1】:

通过增加默认连接超时来解决此问题

Connection('amqp://127.0.0.1/', connect_timeout=15)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多