Paho MQTT 不适用于 Flask(Google 应用引擎)
Paho MQTT not working with Flask(Google app engine)
在我的 Flask(Google 应用程序引擎)应用程序中,我需要向 Mosquitto 代理发布一条消息,该代理 运行 在终端本地连接到端口 1884。
import paho.mqtt.client as mqtt
# ...
try:
# publish.single("Car/Command1", dict, hostname='localhost', port=1884)
client = mqtt.Client('Flask_Publisher')
client.connect("localhost", 1884, 60)
client.publish("mytopic", dict)
client.disconnect()
except Exception as e:
logging.warning("Exception occurred: {}".format(e))
error = e
# ...
I 运行 Mosquitto 代理在终端的 1884 端口本地输入以下命令:sudo mosquitto -p 1884
出于测试目的,我编写了一个 python 脚本,它具有 mqtt 订阅者的角色。
但问题是,当我尝试 运行 Flask 时,它在尝试发布消息时给我以下错误(从异常中捕获):
[Errno 13] Permission denied
编辑:完整回溯
ERROR 2018-08-30 16:47:52,665 wsgi.py:279]
Traceback (most recent call last):
File "/home/santoryu/Scrivania/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_restful/__init__.py", line 273, in error_router
return original_handler(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_restful/__init__.py", line 273, in error_router
return original_handler(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/santoryu/Scrivania/IOT/gae/app/handlers/my_handler.py", line 44, in funzione1
client = mqtt.Client('Flask_Publisher')
File "/home/santoryu/Scrivania/IOT/gae/lib/paho/mqtt/client.py", line 498, in __init__
self._sockpairR, self._sockpairW = _socketpair_compat()
File "/home/santoryu/Scrivania/IOT/gae/lib/paho/mqtt/client.py", line 238, in _socketpair_compat
listensock.bind(("127.0.0.1", 0))
File "/home/santoryu/Scrivania/google-cloud-sdk/platform/google_appengine/google/appengine/dist27/socket.py", line 227, in meth
return getattr(self._sock,name)(*args)
File "/home/santoryu/Scrivania/google-cloud-sdk/platform/google_appengine/google/appengine/api/remote_socket/_remote_socket.py", line 679, in bind
raise _SystemExceptionFromAppError(e)
error: [Errno 13] Permission denied
问题是 App Engine SDK 提供了 Python 的 socket
模块的沙盒版本。当 MQTT 客户端库尝试绑定到本地端口时,它正在使用这个沙盒模块而不是它期望的标准库模块,并且无法绑定到本地主机。
您可以通过在 运行 处于开发模式时将 GAE socket
模块替换为原始标准库模块来解决此问题,从而允许您绑定到本地主机。
在您的 appengine_config.py
文件中:
import os
# Only do the following if we're in development mode
if os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
import imp
import inspect
# Whitelist the ssl and socket modules
from google.appengine.tools.devappserver2.python import sandbox
sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']
# Use the standard library socket module instead
real_os_src_path = os.path.realpath(inspect.getsourcefile(os))
real_socket = os.path.join(os.path.dirname(real_os_src_path), 'socket.py')
imp.load_source('socket', real_socket)
在我的 Flask(Google 应用程序引擎)应用程序中,我需要向 Mosquitto 代理发布一条消息,该代理 运行 在终端本地连接到端口 1884。
import paho.mqtt.client as mqtt
# ...
try:
# publish.single("Car/Command1", dict, hostname='localhost', port=1884)
client = mqtt.Client('Flask_Publisher')
client.connect("localhost", 1884, 60)
client.publish("mytopic", dict)
client.disconnect()
except Exception as e:
logging.warning("Exception occurred: {}".format(e))
error = e
# ...
I 运行 Mosquitto 代理在终端的 1884 端口本地输入以下命令:sudo mosquitto -p 1884
出于测试目的,我编写了一个 python 脚本,它具有 mqtt 订阅者的角色。
但问题是,当我尝试 运行 Flask 时,它在尝试发布消息时给我以下错误(从异常中捕获):
[Errno 13] Permission denied
编辑:完整回溯
ERROR 2018-08-30 16:47:52,665 wsgi.py:279]
Traceback (most recent call last):
File "/home/santoryu/Scrivania/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_restful/__init__.py", line 273, in error_router
return original_handler(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/santoryu/Scrivania/IOT/gae/lib/flask_restful/__init__.py", line 273, in error_router
return original_handler(e)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/santoryu/Scrivania/IOT/gae/lib/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/santoryu/Scrivania/IOT/gae/app/handlers/my_handler.py", line 44, in funzione1
client = mqtt.Client('Flask_Publisher')
File "/home/santoryu/Scrivania/IOT/gae/lib/paho/mqtt/client.py", line 498, in __init__
self._sockpairR, self._sockpairW = _socketpair_compat()
File "/home/santoryu/Scrivania/IOT/gae/lib/paho/mqtt/client.py", line 238, in _socketpair_compat
listensock.bind(("127.0.0.1", 0))
File "/home/santoryu/Scrivania/google-cloud-sdk/platform/google_appengine/google/appengine/dist27/socket.py", line 227, in meth
return getattr(self._sock,name)(*args)
File "/home/santoryu/Scrivania/google-cloud-sdk/platform/google_appengine/google/appengine/api/remote_socket/_remote_socket.py", line 679, in bind
raise _SystemExceptionFromAppError(e)
error: [Errno 13] Permission denied
问题是 App Engine SDK 提供了 Python 的 socket
模块的沙盒版本。当 MQTT 客户端库尝试绑定到本地端口时,它正在使用这个沙盒模块而不是它期望的标准库模块,并且无法绑定到本地主机。
您可以通过在 运行 处于开发模式时将 GAE socket
模块替换为原始标准库模块来解决此问题,从而允许您绑定到本地主机。
在您的 appengine_config.py
文件中:
import os
# Only do the following if we're in development mode
if os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
import imp
import inspect
# Whitelist the ssl and socket modules
from google.appengine.tools.devappserver2.python import sandbox
sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']
# Use the standard library socket module instead
real_os_src_path = os.path.realpath(inspect.getsourcefile(os))
real_socket = os.path.join(os.path.dirname(real_os_src_path), 'socket.py')
imp.load_source('socket', real_socket)