In my python homework, I have to make a server and some clients.
My problem comes from the fixed string size in the packing/unpacking process on both the server and client sides. I want to send messages with two different sized strings.
Here is my simplified code:
client:
import socket
import struct
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(('127.0.0.1', 5555))
str1 = b"A"
msg = (str1, 3)
msg_packed = struct.Struct("1s I").pack(*msg) #the fixed string size is not a problem here
sock.sendall(msg_packed)
reply_packed = sock.recv(1024)
reply = struct.Struct("2s I").unpack(reply_packed) #since the string in the reply can be 'Yes' or 'No' what is 2 and 3 character. I don't know hot make it accept both.
print(reply)
and the Server:
import socket
import select
import struct
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(('0.0.0.0', 5555))
srv.listen()
socks = [srv]
while True:
readable, writeable, err = select.select(socks, [], [], 0.1)
for s in readable:
if s == srv:
client, client_address = srv.accept()
print("New client from: {} address".format(client_address))
socks.append(client)
else:
msg_packed = s.recv(1024)
if msg_packed:
for sock in socks:
if sock == s and sock != srv:
msg = struct.Struct("1s I").unpack(msg_packed)
if (msg[0] == b'A'): #In here the reply message need to be 'Yes' or 'No'
reply = (b'Yes', msg[1] * msg[1])# the struct.Struct("2s I").pack(*reply) will not going to accept this
else:
reply = (b'No', msg[1] + msg[1])
reply_packed = struct.Struct("2s I").pack(*reply)
sock.send(reply_packed)
else:
print("Client disconnected")
socks.remove(s)
s.close()
Is there any way to be able to send both 2 and 3 string lengths? And if yes, how should I change my code?
EDIT: You can just dynamically set the format string of the struct. Here is a simple example:
str1 = b"Yes"
str2 = b"No"
msg_packed1 = struct.Struct("{}s".format(len(str1))).pack(str1)
msg_packed2 = struct.Struct("{}s".format(len(str2))).pack(str2)
In your example it would be
reply_packed = struct.Struct("{}s I".format(len(reply[0]))).pack(*reply)
I got this idea from packing and unpacking variable length array/string using the struct module in python