Sockets...via Zeroconf...In Windows...Gonna lose my mind...

Ooookay, so, a brief synopsis:

I have a plugin I’m working on:

Ignore the “Plex” in the name, I’m aware that’s a no-no.

So this plugin utilizes the pychromecast library, which utilizes the zeroconf python library:

The zeroconf library requires precompiled, OS-specific C-libraries, so I forked it and rewrote it to NOT need that:

So, in a nutshell, the plugin relies on pychromecast, which relies on zeroconf, which uses sockets to send and recieve UDP data on port 5353 (multicast).

Now, with all of this said…the plugin works perfectly on Linux. It’s fairly quick, it gets all of my cast devices reliably, great.

However, on Windows…it doesn’t get any responses back. I recall a similar issue with sockets/mdns between Linux and Windows, and that being fixable by setting the socket flag SO_REUSEADDR a specific way, but it doesn’t fix it in this case.

Additionally, I have taken my modified zeroconf.py file and ensured that it does in fact work in python 2.7 console on the exact same OS, so I’ve ruled out my fixes as being a culprit.

When I wireshark the connections, I see the outgoing request all the time. What’s interesting, however, is that if I look in resource manager between the Plex and Python instances of the request, I’m seeing that Python is listening on 192.168.1.120, whereas PlexScriptHost is listening on IPv4 loopback or IPv4 unspecified.

Even if I set the interface to manually use 192.168.1.120 in zeroconf.py, it doesn’t appear to be binding directly to that interface.

I know this is some pretty low-level stuff, but if I can’t get it to work on Windoze, it’s virtually useless. Anybody have any experience with this?

An update, purely for documentation purposes in case anybody else is silly enough to try something like this:

Added logging to zeroconf, and it is indeed receiving messages. So, the issue must be with how pyChromeCast is handling the incoming data.

Will report back with more…

Update2:

Traced the issue through pychromecast to being in the socket_client.py.

While trying to fetch the status of individual cast devices, it seems to be choking on trying to connect a http socket using UDP calls, or something, and throws and errno 10057. Still investigating, but I also see this error a lot in HomeAssistant’s issue tracker, so I think it’s not just me or Plex…

Okay, last update, as I fixed it.

So, pychromecast, in addition to using Zeroconf’s socket implementation, also uses a standalone socket implementation to connect to the individual cast devices and get their status.

While creating this socket, they’re taking a normal socket and wrapping it in SSL. For some reason, this breaks it. Dropping the ssl bit and making sure to set the SO_REUSEADDR flag seemed to do the trick.

did you manage to get plex to cast to a chrome cast from python? I have been searching all over the internet for answers to this. I found your cast.bundle on Github I am able to load the plex app on the CC but when i send the request_array to the CC the plex app on the tv just comes up saying “sorry something went wrong”

I have a feeling it is somthing to do with the formatting of the json string but i dont seem to be able to find it anywhere :confounded: