Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Google API client breaks name resolution for python paho MQTT in a docker context
Google API client breaks name resolution for python paho MQTT in a docker context [message #1835185] Thu, 26 November 2020 14:56
W S is currently offline W SFriend
Messages: 2
Registered: November 2020
Junior Member
This question was initially posted at (I had to remove http everywhere and replace with XXX because as a new user I cannot post links) stackoverflow.com/questions/65023468/google-api-client-breaks-name-resolution-in-a-docker-context but I feel it may be more relevant in this forum.

My (home grade) code accesses the Google Calendar API and then publishes information it finds there to a MQTT bus (<del>the last part may not be relevant</del> it actually is).

It used to run as a service on my server and I decided to move it into a docker container (also on my server, I run many of them - both foreign ones from Docker Hob and my own).

**The problem I encounter is that calling the Google API libraries apparently breaks name resolution in paho MQTT when ran in a docker container.**

The following code works OK when started from the command line of my server (keys and endpoints redacted)

    import googleapiclient.discovery
    import paho.mqtt.publish
    from google.oauth2 import service_account as google_oauth2_service_account
    
    paho.mqtt.publish.single(
        topic=f"testtopic",
        payload="before google",
        retain=True,
        hostname='mqtt.mydomain'
    )
    print("before google")

    google_credentials = google_oauth2_service_account.Credentials.from_service_account_info(
        {
            "type": "service_account",
            "project_id": "myprojectid",
            "private_key_id": "8...4",
            "private_key": "-----BEGIN PRIVATE KEY-----\nM....Kag==\n-----END PRIVATE KEY-----\n",
            "client_email": "smthg@smthg.iam.gserviceaccount.com",
            "client_id": "1...9",
            "auth_uri": "XXXXs://accounts.google.com/o/oauth2/auth",
            "token_uri": "XXXXs://oauth2.googleapis.com/token",
            "auth_provider_x509_cert_url": "XXXXs://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url": "XXXXs://www.googleapis.com/robot/v1/metadata/x509/XXX%40dXXX.iam.gserviceaccount.com"
        },
        scopes=['XXXXs://www.googleapis.com/auth/calendar'],
        subject='XXX@YYYY'
    )
    google_service = googleapiclient.discovery.build('calendar', 'v3', credentials=google_credentials, cache_discovery=False)
    print("after google")
    
    paho.mqtt.publish.single(
        topic=f"testtopic",
        payload="after google",
        retain=True,
        hostname='mqtt.mydomain'
    )
    print("before end of script")


In an MQTT explorer I see that the topic has been published to twice, with `before google` and `after google`, and the output is

    before google
    after google
    end of script

So far, so good.

I then moved that code file into docker via the following `Dockerfile`:

    FROM ubuntu:20.04
    RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y python3-arrow python3-pip python3-paho-mqtt
    RUN pip3 install google-api-python-client oauth2client
    WORKDIR /app
    COPY . .
    CMD python3 googlecalendartest.py ; sleep 1000


The build went fine but the program crashes with (this is the only log)

    before google
    after google
    Traceback (most recent call last):
      File "googlecalendartest.py", line 32, in <module>
        paho.mqtt.publish.single(
      File "/usr/lib/python3/dist-packages/paho/mqtt/publish.py", line 227, in single
        multiple([msg], hostname, port, client_id, keepalive, will, auth, tls,
      File "/usr/lib/python3/dist-packages/paho/mqtt/publish.py", line 163, in multiple
        client.connect(hostname, port, keepalive)
      File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 937, in connect
        return self.reconnect()
      File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 1071, in reconnect
        sock = self._create_socket_connection()
      File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
        return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
      File "/usr/lib/python3.8/socket.py", line 787, in create_connection
        for res in getaddrinfo(host, port, 0, SOCK_STREAM):
      File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
        for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    socket.gaierror: [Errno -2] Name or service not known


I **do see** that `testtopic` received a payload of `before google`, but the one after the Google API calls is not there anymore.

`socket.gaierror: [Errno -2] Name or service not known` suggests that `mqtt.mydomain` is not resolvable (I think).

**EDIT**: I added a `print(requests.get("h.../google.com").status_code)` call after the Google API part and it works, so resolution is fine.

It means that the resolution is broken only for paho MQTT

[Updated on: Thu, 26 November 2020 17:54]

Report message to a moderator

Previous Topic:V5 API documentation
Next Topic: paho.mqtt-sn.embedded-c
Goto Forum:
  


Current Time: Fri May 03 12:24:15 GMT 2024

Powered by FUDForum. Page generated in 0.03501 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top