From 0cf25ffbe7173be71c0db99c076922d45df956c1 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:03:55 +0100 Subject: [PATCH] Connect sockets Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 43 +++++++++++++++++----- squinnondation/test/squinnondation_test.py | 8 ++-- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index a2e74b0..1b350a4 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -1,27 +1,52 @@ # Copyright (C) 2020 by eichhornchen, ÿnérant # SPDX-License-Identifier: GPL-3.0-or-later - -from argparse import ArgumentParser, Namespace +import socket +from argparse import ArgumentParser +from typing import Any class Squinnondation: - args: Namespace + args: Any + bind_address: str + bind_port: int client_address: str client_port: int def parse_arguments(self) -> None: parser = ArgumentParser(description="MIRC client.") - parser.add_argument('address', type=str, default="localhost", help="Address of the client.") - parser.add_argument('port', type=int, default=2500, help="Port of the client. Must be between 1024 and 65535.") + parser.add_argument('bind_address', type=str, default="localhost", + help="Address of the client.") + parser.add_argument('bind_port', type=int, default=2500, + help="Port of the client. Must be between 1024 and 65535.") + parser.add_argument('--client_address', type=str, default="localhost", + help="Address of the first neighbour.") + parser.add_argument('--client_port', type=int, default=2500, + help="Port of the first neighbour. Must be between 1024 and 65535.") + parser.add_argument('--bind-only', '-b', action='store_true', + help="Don't connect to another client, only listen to connections.") self.args = parser.parse_args() - if not (1024 <= self.args.port <= 65535): - raise ValueError("The port must be between 1024 and 65535.") + if not (1024 <= self.args.bind_port <= 65535) and (1024 <= self.args.client_port <= 65535): + raise ValueError("Ports must be between 1024 and 65535.") - self.client_address = self.args.address - self.client_port = self.args.port + self.bind_address = self.args.bind_address + self.bind_port = self.args.bind_port + self.client_address = self.args.client_address + self.client_port = self.args.client_port @staticmethod def main() -> None: # pragma: no cover instance = Squinnondation() instance.parse_arguments() + + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.bind((instance.bind_address, instance.bind_port)) + + if not instance.args.bind_only: + sock.sendto(b"Hello world!", (instance.client_address, instance.client_port)) + + print(f"Listening on {instance.bind_address}:{instance.bind_port}...") + while True: + data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes + print("received message: %s" % data) + sock.sendto(b"Hello squirrel!", addr) diff --git a/squinnondation/test/squinnondation_test.py b/squinnondation/test/squinnondation_test.py index 1c38405..0e88607 100644 --- a/squinnondation/test/squinnondation_test.py +++ b/squinnondation/test/squinnondation_test.py @@ -8,7 +8,7 @@ from squinnondation.squinnondation import Squinnondation class TestSquinnondation(unittest.TestCase): - def test_arguments(self): + def test_arguments(self) -> None: """ Parse arguments and check errors. """ @@ -22,7 +22,9 @@ class TestSquinnondation(unittest.TestCase): self.assertRaises(ValueError, squinnondation.parse_arguments) # Esnure that the parsing is correct - sys.argv = ["squinnondation", "localhost", "4242"] + sys.argv = ["squinnondation", "localhost", "4242", "--client_address", "localhost", "--client_port", "2500"] squinnondation.parse_arguments() + self.assertEqual(squinnondation.bind_address, "localhost") + self.assertEqual(squinnondation.bind_port, 4242) self.assertEqual(squinnondation.client_address, "localhost") - self.assertEqual(squinnondation.client_port, 4242) + self.assertEqual(squinnondation.client_port, 2500)