New: the AI firewall watches the whole platform and explains each incident in plain words. See how it works

Python call to the latest-values endpoint hangs for two minutes on our Pi

Python call to the latest-values endpoint hangs for two minutes on our Pi

Solved 4 4

I run a little lobby screen at our hotel, a Pi 4 that shows the kitchen fridge temperatures. A Python script asks Hermetry for the latest values every 5 minutes with an API key (Read only preset):

GET /tenant/<tenantId>/api/telemetry/latest?kind=iot_device&entityId=<deviceId>&keys=temperature
Authorization: Bearer sk_live_...

Most calls take about 2 minutes, some time out, then sometimes one is instant. From my laptop on the same Wi-Fi it is always instant. I tried curl on the Pi to compare:

curl -4 ... -> 0.3 s
curl -6 ... -> hangs, then "Failed to connect"

Our ISP hands out IPv6 since a router swap last month. I don’t know much about IPv6, can I just make the Pi use the old way?

QoS 1 and a lot of patience.
♥ 1

Could it be the API key’s IP allowlist blocking you sometimes?

♥ 2

That would give you a 401 or 403 straight away, not a hang. Easiest is to turn IPv6 off on the Pi with sysctl.

Ground loops are my cardio.
♥ 0

Had exactly this at one of our properties after the ISP turned IPv6 on. The Pi gets an IPv6 address, tries it first, the route is broken and it waits for the timeout before falling back to IPv4. Your curl test shows that.

I didn’t want to switch IPv6 off for everything, so I told the Pi to prefer IPv4. Uncomment this line in /etc/gai.conf:

precedence ::ffff:0:0/96  100

No reboot needed for new processes, just restart your script. If you only want it for the script, you can force it in Python before importing requests:

import socket
import urllib3.util.connection as conn
conn.allowed_gai_family = lambda: socket.AF_INET

And Owen’s idea is still a good one for later. Put the hotel’s public IPv4 in the key’s IP allowlist so a stolen key is useless elsewhere.

Accepted solution♥ 6

The gai.conf line did it, every call is under a second now. I also added our public IPv4 to the key’s allowlist like you said. Thanks David!

QoS 1 and a lot of patience.
♥ 0
Viewing 5 posts – 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Join the conversation

Sign in to ask and answer, like helpful posts and mark the reply that solved your problem. Every contribution earns points and levels.

Share