Ryan Ponsell
Ryan Ponsell

Reputation: 55

Error when trying to send UDP over network

I am attempting to communicate with an ETC EOS lighting console with a raspberry pi over a local network.

This is the code I am currently using:

import socket

UDP_IP = "10.1.10.149"
UDP_PORT = "0"
MESSAGE = "$ Channel 1 @ FULL #"

print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

The error I am receiving is

Traceback (most recent call last):
  File "/home/pi/Desktop/eoscontrol.py", line 13, in <module>
    sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
TypeError: a bytes-like object is required, not 'str'

How can I change my string to bytes to resolve this error?

I am very new to python, I'd bet this is something pretty simple, I just don't know it well yet.

Thanks!

Upvotes: 0

Views: 209

Answers (1)

thenullptr
thenullptr

Reputation: 411

You need to the send data in byte format by encoding it first. The below should work for you

sock.sendto(bytes(MESSAGE, 'utf-8'), (UDP_IP, UDP_PORT)) # 

Upvotes: 1

Related Questions