Я пытаюсь отправить словарь через клиентский сокет, но рано или поздно я получаю эту ошибку.Понятия не имею, как это исправить, есть предложения?
Моя ОС: Linux Mint 19.1 Python: 3.6.7
Я пробовал это: https://stackoverflow.com/a/38526115/11434238, но без результатов
сервер:
def accept_connection(conn, sel):
client, addr = conn.accept()
client.setblocking(0)
print(f'New connection from {addr[0]}:{addr[1]}.')
sel.register(client, client_events, client_data)
def handle_connection(key, mask, sel):
client = key.fileobj
# data = key.data
if mask & selectors.EVENT_READ:
header = int(client.recv(5).decode('utf-8').strip())
if header == 0:
# client leaved server or connection broken
print('Closing connection')
sel.unregister(client)
client.close()
else:
received = client.recv(header)
print(header, received, sep="\n")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
server.bind((HOST ,PORT))
server.listen(SLOTS)
selector.register(server, selectors.EVENT_READ, data = None)
while True:
events = selector.select()
for key, mask in events:
sock = key.fileobj
if key.data is None:
accept_connection(sock, selector)
else:
handle_connection(key, mask, selector)
клиент:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
sock.connect_ex((HOST, PORT))
info = {
"id": 1,
"x": 10,
"y": 10
}
def send_data(socket = sock):
data = bytes(json.dumps(info), 'utf-8')
header = bytes(f'{len(data):<5}', 'utf-8')
print(header)
socket.send(header)
socket.send(data)
while True:
send_data()