Sending UDP packet to 255.255.255.255 using Python

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
HomeAutomationPlugins.com
Posts: 41
Joined: Sat Feb 18, 2012 12:38 pm

Sending UDP packet to 255.255.255.255 using Python

Post by HomeAutomationPlugins.com »

Hi,

I'm hoping someone who's better at Python than me (not hard!) can help with this.

I want to send a packet to the broadcast address 255.255.255.255, but am getting this error:

Code: Select all

LightwaveRF Error               Error in plugin execution ExecuteAction:

Traceback (most recent call last):
  File "plugin.py", line 118, in actionControlDimmerRelay
  File "plugin.py", line 42, in sendMessageToDevice
  File "plugin.py", line 50, in sendMessage
<class 'socket.error'>: (13, 'Permission denied')
The same code works fine when I specify an actual IP address, here's the code that's sending the packet:

Code: Select all

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.sendto(msg, ("255.255.255.255", 9760))
Many thanks!
HomeAutomationPlugins.com
Posts: 41
Joined: Sat Feb 18, 2012 12:38 pm

Re: Sending UDP packet to 255.255.255.255 using Python

Post by HomeAutomationPlugins.com »

Figured it out!

Just needed the add the following line to allow broadcast:

Code: Select all

sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
so the complete code block is:

Code: Select all

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
		sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
		sock.sendto(msg, (self.pluginPrefs["wifilinkip"], self.lightwaveRfUdpPort))
Locked

Return to “Extending Indigo with Plugins and Python”