Connect sockets

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:03:55 +01:00
parent bd6a8fc431
commit 0cf25ffbe7
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 39 additions and 12 deletions

View File

@ -1,27 +1,52 @@
# Copyright (C) 2020 by eichhornchen, ÿnérant # Copyright (C) 2020 by eichhornchen, ÿnérant
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import socket
from argparse import ArgumentParser, Namespace from argparse import ArgumentParser
from typing import Any
class Squinnondation: class Squinnondation:
args: Namespace args: Any
bind_address: str
bind_port: int
client_address: str client_address: str
client_port: int client_port: int
def parse_arguments(self) -> None: def parse_arguments(self) -> None:
parser = ArgumentParser(description="MIRC client.") parser = ArgumentParser(description="MIRC client.")
parser.add_argument('address', type=str, default="localhost", help="Address of the client.") parser.add_argument('bind_address', type=str, default="localhost",
parser.add_argument('port', type=int, default=2500, help="Port of the client. Must be between 1024 and 65535.") 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() self.args = parser.parse_args()
if not (1024 <= self.args.port <= 65535): if not (1024 <= self.args.bind_port <= 65535) and (1024 <= self.args.client_port <= 65535):
raise ValueError("The port must be between 1024 and 65535.") raise ValueError("Ports must be between 1024 and 65535.")
self.client_address = self.args.address self.bind_address = self.args.bind_address
self.client_port = self.args.port self.bind_port = self.args.bind_port
self.client_address = self.args.client_address
self.client_port = self.args.client_port
@staticmethod @staticmethod
def main() -> None: # pragma: no cover def main() -> None: # pragma: no cover
instance = Squinnondation() instance = Squinnondation()
instance.parse_arguments() 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)

View File

@ -8,7 +8,7 @@ from squinnondation.squinnondation import Squinnondation
class TestSquinnondation(unittest.TestCase): class TestSquinnondation(unittest.TestCase):
def test_arguments(self): def test_arguments(self) -> None:
""" """
Parse arguments and check errors. Parse arguments and check errors.
""" """
@ -22,7 +22,9 @@ class TestSquinnondation(unittest.TestCase):
self.assertRaises(ValueError, squinnondation.parse_arguments) self.assertRaises(ValueError, squinnondation.parse_arguments)
# Esnure that the parsing is correct # 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() 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_address, "localhost")
self.assertEqual(squinnondation.client_port, 4242) self.assertEqual(squinnondation.client_port, 2500)