Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding flask mqtt example that uses tls/ssl #117

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ ENV/
*.sublime*
doc/_build/*
*.crt
client.key
.mypy_cache/*
venv_*/

Pipfile.log

# IDE configuration
.idea/
.vscode/
.vscode/
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ mqtt3 = Mqtt(app, config_prefix="MQTT3")
```

### Small publish/subscribe MQTT client
Note: if you want to enable ssl transport layer on the mqtt communication
take a look at the commented sections in the example file.
You may need to add or replace some of the MQTT_TLS options and adjust to your needs.

```python
"""
Expand All @@ -160,6 +163,8 @@ from flask import Flask, render_template
from flask_mqtt import Mqtt
from flask_socketio import SocketIO
from flask_bootstrap import Bootstrap
# if SSL enabled
# from flask_mqtt import ssl

eventlet.monkey_patch()

Expand All @@ -180,6 +185,17 @@ app.config['MQTT_CLEAN_SESSION'] = True
# app.config['MQTT_TLS_INSECURE'] = True
# app.config['MQTT_TLS_CA_CERTS'] = 'ca.crt'

# Parmeters for SSL with client certificate
# app.config['MQTT_TLS_ENABLED'] = True
# app.config['MQTT_BROKER_URL'] = 'test.mosquitto.org'
# app.config['MQTT_BROKER_PORT'] = 8884
# app.config['MQTT_TLS_INSECURE'] = False
# app.config['MQTT_TLS_CA_CERTS'] = 'mosquitto.org.crt'
# app.config['MQTT_TLS_CERTFILE'] = 'client.crt'
# app.config['MQTT_TLS_KEYFILE'] = 'client.key'
# app.config['MQTT_TLS_VERSION'] = ssl.PROTOCOL_TLSv1_2
# app.config['MQTT_TLS_CERT_REQS'] = True

mqtt = Mqtt(app)
socketio = SocketIO(app)
bootstrap = Bootstrap(app)
Expand Down
14 changes: 14 additions & 0 deletions example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from flask_mqtt import Mqtt
from flask_socketio import SocketIO
from flask_bootstrap import Bootstrap
# if SSL enabled
# from flask_mqtt import ssl

eventlet.monkey_patch()

Expand All @@ -35,6 +37,18 @@
# app.config['MQTT_TLS_INSECURE'] = True
# app.config['MQTT_TLS_CA_CERTS'] = 'ca.crt'

# Parmeters for SSL with client certificate
# app.config['MQTT_TLS_ENABLED'] = True
# app.config['MQTT_BROKER_URL'] = 'test.mosquitto.org'
# app.config['MQTT_BROKER_PORT'] = 8884
# app.config['MQTT_TLS_INSECURE'] = False
# app.config['MQTT_TLS_CA_CERTS'] = 'mosquitto.org.crt'
# app.config['MQTT_TLS_CERTFILE'] = 'client.crt'
# app.config['MQTT_TLS_KEYFILE'] = 'client.key'
# app.config['MQTT_TLS_VERSION'] = ssl.PROTOCOL_TLSv1_2
# app.config['MQTT_TLS_CERT_REQS'] = True


mqtt = Mqtt(app)
socketio = SocketIO(app)
bootstrap = Bootstrap(app)
Expand Down
4 changes: 2 additions & 2 deletions example/environment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: /Users/stefan/Documents/python/projects/Flask-MQTT/example/venv
name: venv
channels:
- conda-forge
- pytorch
Expand Down Expand Up @@ -40,5 +40,5 @@ dependencies:
- python-socketio==4.4.0
- typing==3.7.4.1
- visitor==0.1.3
prefix: /Users/stefan/Documents/python/projects/Flask-MQTT/example/venv
prefix: venv

15 changes: 8 additions & 7 deletions example/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
bidict==0.22.1
certifi==2023.7.22
Click==7.0
dnspython==1.16.0
dominate==2.4.0
eventlet==0.25.1
eventlet==0.33.2
Flask==2.3.2
Flask-Bootstrap==3.3.7.1
Flask-MQTT==1.0.5
Flask-SocketIO==3.3.2
greenlet==0.4.15
Flask-MQTT==1.1.1
Flask-SocketIO==5.3.2
greenlet==2.0.1
itsdangerous==1.1.0
Jinja2==2.11.1
Jinja2==2.11.3
MarkupSafe==1.1.1
monotonic==1.5
paho-mqtt==1.5.0
python-engineio==3.11.2
python-socketio==4.4.0
python-engineio==4.3.4
python-socketio==5.7.2
six==1.14.0
typing==3.7.4.1
visitor==0.1.3
Expand Down
4 changes: 2 additions & 2 deletions example/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ <h3 class="panel-title">Publish MQTT Message</h3>
<option value=1>1</option>
<option value=2>2</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-4">
Expand Down Expand Up @@ -120,7 +120,7 @@ <h3 class="panel-title">Subscribe MQTT Messages</h3>
<option value=1>1</option>
<option value=2>2</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-4">
Expand Down