我一直在尝试连接到代理端口1884。但是每次拒绝连接都会出错。
def __init__(self, host='127.0.0.1', port=8080, broker_address='127.0.0.1', broker_port=1884, start_config=None):
self.app = Bottle()
"""Create the http service app"""
self.broker = SimpleBroker(address=broker_address, port=broker_port)
self.broker.start()
"""Create and run the broker (broker always runs as a daemon)"""
self.brokerAddress = broker_address
"""address of the API broker - usually identical to own address, but others for testing"""
self.mqttc = Client()
"""mqtt client for publishing messages to API broker - delivered to edge and preprocessing layer"""
self.mqttc.connect(host=broker_address, port=broker_port)
"""connect to the broker upon startup"""
然后是执行部分:
if __name__ == "__main__":
myAPI = APIServer('127.0.0.1', 8080, '127.0.0.1', start_config='all wheels')
myAPI.start()
错误消息:
Traceback (most recent call last):
File "/root/PycharmProjects/Thesis2019/API/APIServer.py", line 328, in <module>
myAPI = APIServer('127.0.0.1', 8080, '127.0.0.1', start_config='all wheels')
File "/root/PycharmProjects/Thesis2019/API/APIServer.py", line 42, in __init__
self.mqttc.connect(host=broker_address, port=broker_port)
File "/root/PycharmProjects/Thesis2019/venv/lib/python3.6/site-packages/paho/mqtt/client.py", line 839, in connect
return self.reconnect()
File "/root/PycharmProjects/Thesis2019/venv/lib/python3.6/site-packages/paho/mqtt/client.py", line 962, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
另一方面,当我在调试模式下执行它时,我得到了:
[2019-04-09 23:43:05,243] :: WARNING :: hbmqtt.broker.plugins.topic_taboo :: 'topic-check' section not found in context configuration
[2019-04-09 23:43:05,243] :: INFO :: transitions.core :: Exited state new
[2019-04-09 23:43:05,244] :: INFO :: transitions.core :: Entered state starting
[2019-04-09 23:43:05,248] :: INFO :: hbmqtt.broker :: Listener 'default' bind to 127.0.0.1:1884 (max_connections=100)
[2019-04-09 23:43:05,248] :: INFO :: transitions.core :: Exited state starting
[2019-04-09 23:43:05,248] :: INFO :: transitions.core :: Entered state started
除了所有拒绝连接的部分之外,我猜它只是在调试模式下启动就可以了吗?
它在Windows上有效,但在Mac或Linux上不可用
为什么每次尝试执行连接时都会出现连接被拒绝的错误?我想念什么?
预先感谢您的想法。
答案 0 :(得分:0)
解决方案是:
在配置文件中,我试图通过端口1884进行连接的Mosquitto默认端口连接设置为1883。
我认为,每当更改数字时都会设置此属性。这是不正确的,因为它们是静态的。不会动态变化。(确保也有办法。)
除此之外,还必须交换API Broker和Broker端口号。我不知道为什么,但是Windows可以毫无问题地绑定在它上面。但是Mac OS和Linux会抛出“连接被拒绝”错误。
在发疯之前,这些仍然是有效的检查。 90%的问题是由于这些问题之一引起的。
如果您还有其他帮助或问题,可以在这里评论,我将尽力为将来的查询提供帮助。