python – TimeoutError: timed out while using public broker in mqtt

When I use a local broker, namely localhost, the program can run, but when I use a public broker such as the Eclipse Project, the program cannot run and has an error

TimeoutError: timed out

Can someone help to fix the error?
Here is the code:

import paho.mqtt.client as mqtt
import time
import datetime

def on_message(client, userdata, message):
    print("Message received: ", message.payload.decode("utf-8"))

broker_address = "mqtt.eclipse.org"
client = mqtt.Client("P1")
client.on_message = on_message

client.connect(broker_address, port=1883)  # The default MQTT port is 1883

client.loop_start()

while True:
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    client.publish("house/bulbs/bulb1", current_time)
    print(f"Published: {current_time}")
    time.sleep(1)

client.loop_stop()

Read more here: Source link