Forums

CloudMQTT Connection Refused

Hello! I am a complete beginner with pythonanywhere (and kind of with python, too). I am working on a Internet of Things group project at school and I was thinking about using pythonanywhere as a server kind of thing that could take messages from my CloudMQTT message queue (that is already working) and make JSON out of them and save them to a database/file.

Maybe it is a stupid approach, we are learning ))

Anyways, I uploaded my python script (that connects to the MQTT queue) on pythonanywhere, installed and imported the paho.mqtt.client library (successfully) but when I run the script, it gives an error message:

13:53 ~ $ python2.7 mqtt.py
Traceback (most recent call last):                                                                                                                          
  File "mqtt.py", line 37, in <module>                                                                                                                      
    mqttc.connect("m20.cloudmqtt.com", "36348")                                                                                                             
  File "/home/PetrKryze/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 768, in connect                                                       
    return self.reconnect()                                                                                                                                 
  File "/home/PetrKryze/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 895, in reconnect                                                     
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))                                                       
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection                                                                                       
    raise err                                                                                                                                               
socket.error: [Errno 111] Connection refused

Could anyone help or give some tips? Thanks alot!

For free accounts making outbound connections, you can only make http/https connections to a whitelist.

So just upgrading the account will solve the problem? Thanks

Yes, it will

Hi, I have had the same problem using mqtt as a client and have upgraded to a paid account and I still get the connection refused error.

File "Upload_ttn-sub-temp.py", line 72, in <module> client.connect(TTN_SERVER, keepalive=30) File "/home/paulneumeyer/.virtualenvs/loraenv/lib/python3.6/site-packages/paho/mqtt/client.py", line 768, in connect return self.reconnect() File "/home/paulneumeyer/.virtualenvs/loraenv/lib/python3.6/site-packages/paho/mqtt/client.py", line 895, in reconnect sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0)) File "/usr/lib/python3.6/socket.py", line 722, 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

What is the expected delay from payment to the ports being open?

I'm not sure if the only way to getting it running is in a Web application. I run the script from a Bash console and it fails. I run the same virtualenv from a Web application and run the same script and it runs.

Do I need to recreate the bash console now I have paid?

yes, you need a new Bash console to get the updated Internet settings. I think we say as much on the final upgrade screen...

Can you share a pythonanywhere example of connecting to Cloudmqtt?

Have you seen this?

Yes, but the problem is that we need an example with the flask framework and on top of websocket. So that it can run in pythonanywhere.

from flask import Flask from flask_mqtt import Mqtt

app = Flask(__name__)
app.config['SECRET'] = 'my secret key'
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['MQTT_BROKER_URL'] = "m23.cloudmqtt.com"  # use the free broker from HIVEMQ
app.config['MQTT_BROKER_PORT'] = 18353  # default port for non-tls connection
app.config['MQTT_USERNAME'] = '...'  # set the username here if you need authentication for the broker
app.config['MQTT_PASSWORD'] = '...'  # set the password here if the broker demands authentication
app.config['MQTT_CLIENT_ID'] = 'serverapp'
#app.config['MQTT_CLEAN_SESSION'] = True
app.config['MQTT_KEEPALIVE'] = 30
app.config['MQTT_TLS_ENABLED'] = False
app.config['MQTT_LAST_WILL_TOPIC'] = 'home/data'
app.config['MQTT_LAST_WILL_MESSAGE'] = 'bye'
app.config['MQTT_LAST_WILL_QOS'] = 2

mqtt = Mqtt(app)
mqtt.subscribe('data')

@app.route('/')
def index():
    return 'OK'

@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
    print('!!! received: %s' % message.payload.decode())

This simple example connects to the queue, notifies you of disconnection. But the message on the topic that subscribes does not receive.

Oh sorry for the confusion! the websocket stuff wouldn't work with our webapps. If there is a way for cloudmqtt to hit an endpoint etc over http, then that could work, but not websockets.