essentially the message being sent is built up as:
[length of opcode (in bytes)] - [length of message (in byes)] - [opcode] - [message]
where each section above is a utf-8 encoded strings.
My original python code was:
HEADER = 256 #header length(bytes)
OPCODE = 4
PORT = #####REDACTED_FOR_PRIVACY#####
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = #####REDACTED_FOR_PRIVACY#####
ADDR = (SERVER, PORT)
class Opcode (str, Enum):
WEEK: str = "1"
EMAIL: str = "2"
COMPETITION_DURATION: str = "3"
SPOTIFY: str = "5"
SUBMISSION_DURATION: str = "6"
RANKING_DURATION: str = "7"
DISCONNECT: str = "8"
NEW_PLAYLIST: str = "9"
NUM_PARTICIPANTS: str = "10"
NAME_QUESTION: str = "11"
BONUS_QUESTION: str = "12"
def connect_to_server():
global client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(opcode, msg):
message= msg.encode(FORMAT) #convert the message to byte format
op = opcode.encode(FORMAT)
msg_length = len(message) #length of message
msg_send_length = str(msg_length).encode(FORMAT) #convert the length of the message to byte format
op_length = len(op)
op_send_length = str(op_length).encode(FORMAT)
# Pad out the length to 64 bytes and the opcode to 4 bytes
msg_send_length += b' ' * (HEADER - len(msg_send_length))
op_send_length += b' ' * (OPCODE - len(op_send_length))
client.send(msg_send_length)
client.send(op_send_length)
client.send(op)
client.send(message)
return client.recv(2048).decode(FORMAT)
send(Opcode.EMAIL, "example@gmail.com")
send(Opcode.WEEK, "12")
send(Opcode.COMPETITION_DURATION, "7")
send(Opcode.SPOTIFY, "https://open.spotify.com/playlist/cyt67ryic657bi")