-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol_handling.py
More file actions
27 lines (20 loc) · 977 Bytes
/
protocol_handling.py
File metadata and controls
27 lines (20 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""
Contains definitions and utilities for handling both TCP and UDP
"""
if __name__ == '__main__':
raise SystemExit(f"Running '{__file__.split('/')[-1]}' directly is unsupported.")
import socket
from typing import Union, Literal
from argparse import ArgumentTypeError
# Define shorthand connection modes
_TCP = socket.SOCK_STREAM
_UDP = socket.SOCK_DGRAM
_MODE_TYPEHINT = Union[Literal[_TCP], Literal[_UDP]]
# DEFAULT VALUES <THESE MAY BE EDITED TO YOUR HEART'S CONTENT>
# MAY BE CHANGED BY COMMAND LINE ARGUMENTS
CONNECTION_PROTOCOL_DEFAULT: _MODE_TYPEHINT = _TCP
def _get_connection_mode(value: str) -> _MODE_TYPEHINT:
""" Checks that a valid connection protocol is specified, and returns the corresponding socket type """
if (_mode := (_valid_modes := {'udp': _UDP, 'tcp': _TCP}).get(value.lower(), ...)) is ...:
raise ArgumentTypeError(f"'{value}' is not a valid connection mode. Choices are: {tuple(_valid_modes.keys())}.")
return _mode