mirror of
https://github.com/Xevion/tcp-chat.git
synced 2025-12-06 09:16:40 -06:00
commit rest of old client/server fixes
This commit is contained in:
14
helpers.py
14
helpers.py
@@ -1,7 +1,19 @@
|
||||
import json
|
||||
|
||||
HEADER_LENGTH = 10
|
||||
|
||||
|
||||
def prepare(message: str, encoding='ascii') -> bytes:
|
||||
def prepare(message: str, encoding='utf-8') -> bytes:
|
||||
"""Prepares a message for sending through a socket by adding a proper header and encoding it."""
|
||||
header = f'{len(message):<{HEADER_LENGTH}}'
|
||||
return (header + message).encode(encoding)
|
||||
|
||||
|
||||
def prepare_json(object) -> bytes:
|
||||
"""
|
||||
Prepares a object for sending as encoded JSON with a header.
|
||||
|
||||
:param object: A JSON-encodable object
|
||||
:return: Encoded JSON
|
||||
"""
|
||||
return prepare(json.dumps(object))
|
||||
|
||||
Reference in New Issue
Block a user