How to communicate with /run/snapd.socket using Python?

If you’re willing to move beyond the Python standard library, snapd-glib is probably the best option. On Ubuntu, you can install the necessary parts with:

sudo apt install python3-gi gir1.2-snapd-1

The binding uses gobject-introspection, so you can import it like so:

import gi
gi.require_version('Snapd', '1')
from gi.repository import Snapd

You can list available snaps with something like this:

c = Snapd.Client()
for snap in c.list_sync():
    print(snap.props.name)

You can poke around interactively with help() to discover how to use the API.