eclipse-paho / eclipse-paho/paho.mqtt.python
open connection with custom socket
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 742
- Avg merge
- 12d 48m
- Merged PRs (30d)
- 1
Description
# Feature Description
I would like to be able to connect with a manually provided socket.
This would enable me to tunnel MQTT through an SSH connection with [paramiko](https://docs.paramiko.org/en/stable/api/transport.html#paramiko.transport.Transport.open_channel).
```python3
import paramiko
# connect to SSH gateway
sshclient = paramiko.SSHClient()
sshclient.connect("sshgateway.example.org", "user", "password")
def create_socket():
# open tunnel to the internal MQTT port
return sshclient.get_transport().open_channel('direct-tcpip', ("127.0.0.1", 1883), ('', 0))
import paho.mqtt.client as mqtt
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.connect("127.0.0.1", transport="callback", create_socket=create_socket)
```
# Requested Solution
```python3
import socket
def create_socket():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('mqtt.example.org', 1883))
return sock
import paho.mqtt.client as mqtt
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.connect("mqtt.example.org", transport="callback", create_socket=create_socket)
```
# Alternatives
For now, I can use my own SocketedClient class, which inherits Client and uses a provided create_socket function on connect() and reconnect().
Contributor guide
Assessment
This issue has not been assessed yet.