[Python] CouchDB Connection Refused Error on Jetstream2 Access Server (Ubuntu 20.04 LTS)

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I'm running a Python script that connects to a CouchDB database and subscribes to an MQTT topic. The script works fine on my laptop (Ubuntu 24.04.1 LTS), but when I try to run it on a Jetstream2 Access server (Ubuntu 20.04 LTS), I get a connection refused error when trying to access/create a database. Here's the relevant part of my code:

    import paho.mqtt.client as mqtt
    import couchdb
    import json
    from datetime import datetime
    from urllib.parse import quote

    # CouchDB setup
    username = "admin"
    password = "" # Your actual password
    host = "localhost"
    port = "5984"

    # URL encode the username and password
    encoded_username = quote(username, safe='')
    encoded_password = quote(password, safe='')

    couch_url = f"http://{encoded_username}:{encoded_password}@{host}:{port}"

    try:
    couch = couchdb.Server(couch_url)
    print("Successfully connected to CouchDB")
    except Exception as e:
    print(f"Error connecting to CouchDB: {e}")
    exit(1)

    db_name = 'weather_data'
    try:
    if db_name not in couch:
    db = couch.create(db_name)
    else:
    db = couch[db_name]
    print(f"Successfully accessed/created database: {db_name}")
    except Exception as e:
    print(f"Error accessing/creating database: {e}")
    exit(1)

    # MQTT setup
    mqtt_broker = "iotwx.ucar.edu"
    mqtt_port = 1883
    # mqtt_topic = "ncar/iotwx/hi/maui/mauiNNN"
    mqtt_topic = "ncar/iotwx/co/rmnp/neon000"


    def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")
    client.subscribe(mqtt_topic)

    def on_message(client, userdata, msg):
    print(f"Received message on topic: {msg.topic}")
    payload = msg.payload.decode()
    print(f"Raw payload: {payload}")

    lines = payload.split('\n')

    data = {}
    for line in lines:
    if ':' in line:
    key, value = line.split(':', 1)
    data[key.strip()] = value.strip()

    print(f"Parsed data: {data}")

    if 'sensor' in data and 'm' in data and 't' in data:
    doc = {
    'device': data.get('device', ''),
    'sensor': data['sensor'],
    'measurement': float(data['m']),
    'timestamp': int(data['t']),
    'received_at': datetime.now().isoformat()
    }
    print(f"Prepared document for CouchDB: {doc}")
    try:
    doc_id, doc_rev = db.save(doc)
    print(f"Saved document to CouchDB. ID: {doc_id}, Rev: {doc_rev}")
    except Exception as e:
    print(f"Error saving document to CouchDB: {e}")
    else:
    print("Received message does not contain all required fields (sensor, m, t)")

    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message

    try:
    client.connect(mqtt_broker, mqtt_port, 60)
    print(f"Connected to MQTT broker: {mqtt_broker}")
    except Exception as e:
    print(f"Error connecting to MQTT broker: {e}")
    exit(1)

    print(f"Listening for messages on topic: {mqtt_topic}")
    client.loop_forever()



    The error message I get is:

    Successfully connected to CouchDB
    Error accessing/creating database: [Errno 111] Connection refused


    I'm not sure what's causing the connection refused error, especially since the script works fine on my laptop. Has anyone else encountered this issue on Jetstream2 Access servers or Ubuntu 20.04 LTS? Any help would be appreciated!

    Environment:
    Jetstream2 Access server (Ubuntu 20.04 LTS)
    CouchDB 3.2.2
    Python 3.8.10
    couchdb library version 1.1.1

    Continue reading...

Compartilhe esta Página