diff --git a/hw_Lesson_03/client/__main__.py b/hw_Lesson_03/client/__main__.py new file mode 100644 index 0000000..60d1a9d --- /dev/null +++ b/hw_Lesson_03/client/__main__.py @@ -0,0 +1,52 @@ +from argparse import ArgumentParser +import socket +import datetime +import jim +import settings + + +def get_presence_msg(): + time = datetime.datetime.now() + msg = { + "action": "presence", + "time": time.isoformat(), + "type": "status", + "user": { + "account_name": "anonim", + "status": "Yep, I am here!" + } + } + + return jim.pack(msg) + + +host = getattr(settings, 'HOST', '127.0.0.1') +port = getattr(settings, 'PORT', 7777) + +parser = ArgumentParser() +parser.add_argument('-a', '--addr', type=str, help='Sets ip address') +parser.add_argument('-p', '--port', type=int, help='Sets port') + +args = parser.parse_args() + +if args.ip: + host = args.ip +if args.port: + port = args.port + +try: + sock = socket.socket() + sock.connect((host, port)) + print(f'Client started with {host}:{port}') + while True: + msg = get_presence_msg() + sock.sendall(msg) + response = sock.recv(settings.BUFFERSIZE) + response = jim.unpack(response) + print('Got next response from server:', response) + +except KeyboardInterrupt: + print('Client closed') + + + diff --git a/hw_Lesson_03/client/__pycache__/jim.cpython-36.pyc b/hw_Lesson_03/client/__pycache__/jim.cpython-36.pyc new file mode 100644 index 0000000..60973c6 Binary files /dev/null and b/hw_Lesson_03/client/__pycache__/jim.cpython-36.pyc differ diff --git a/hw_Lesson_03/client/__pycache__/settings.cpython-36.pyc b/hw_Lesson_03/client/__pycache__/settings.cpython-36.pyc new file mode 100644 index 0000000..309e4ef Binary files /dev/null and b/hw_Lesson_03/client/__pycache__/settings.cpython-36.pyc differ diff --git a/hw_Lesson_03/client/jim.py b/hw_Lesson_03/client/jim.py new file mode 100644 index 0000000..88c01a8 --- /dev/null +++ b/hw_Lesson_03/client/jim.py @@ -0,0 +1,21 @@ +import json + + +def pack(dict_msg): + """ + Создание сообщения, пригодного для отправки через TCP + :param dict_msg: dict + :return: str + """ + str_msg = json.dumps(dict_msg) + return str_msg.encode('utf-8') + + +def unpack(bt_str): + """ + Распаквка полученного сообщения + :param bt_str: str + :return: dict + """ + str_decoded = bt_str.decode('utf-8') + return json.loads(str_decoded) \ No newline at end of file diff --git a/hw_Lesson_03/client/settings.py b/hw_Lesson_03/client/settings.py new file mode 100644 index 0000000..0107932 --- /dev/null +++ b/hw_Lesson_03/client/settings.py @@ -0,0 +1,3 @@ +HOST = "127.0.0.1" +PORT = 7777 +BUFFERSIZE = 1024 diff --git a/hw_Lesson_03/server/__main__.py b/hw_Lesson_03/server/__main__.py new file mode 100644 index 0000000..2216496 --- /dev/null +++ b/hw_Lesson_03/server/__main__.py @@ -0,0 +1,44 @@ +from argparse import ArgumentParser +import socket +import datetime +import jim +import settings + + +def get_response_msg(): + msg = { + "response": 200, + "alert": "Необязательное сообщение/уведомление" + } + return jim.pack(msg) + + +host = getattr(settings, 'HOST', '127.0.0.1') +port = getattr(settings, 'PORT', 7777) + +parser = ArgumentParser() +parser.add_argument('-a', '--addr', type=str, help='Sets ip address') +parser.add_argument('-p', '--port', type=int, help='Sets port') + +args = parser.parse_args() + +if args.ip: + host = args.ip +if args.port: + port = args.port + +try: + sock = socket.socket() + sock.bind((host, port)) + sock.listen(5) + print(f'Server started with {host}:{port}') + while True: + client, address = sock.accept() + print(f'Client detected { address }') + request = client.recv(settings.BUFFERSIZE) + request = jim.unpack(request) + print('Got next request from client:', request) + msg = get_response_msg() + client.sendall(msg) +except KeyboardInterrupt: + print('Server closed') diff --git a/hw_Lesson_03/server/__pycache__/jim.cpython-36.pyc b/hw_Lesson_03/server/__pycache__/jim.cpython-36.pyc new file mode 100644 index 0000000..5bb19cc Binary files /dev/null and b/hw_Lesson_03/server/__pycache__/jim.cpython-36.pyc differ diff --git a/hw_Lesson_03/server/__pycache__/settings.cpython-36.pyc b/hw_Lesson_03/server/__pycache__/settings.cpython-36.pyc new file mode 100644 index 0000000..a95f3b8 Binary files /dev/null and b/hw_Lesson_03/server/__pycache__/settings.cpython-36.pyc differ diff --git a/hw_Lesson_03/server/jim.py b/hw_Lesson_03/server/jim.py new file mode 100644 index 0000000..88c01a8 --- /dev/null +++ b/hw_Lesson_03/server/jim.py @@ -0,0 +1,21 @@ +import json + + +def pack(dict_msg): + """ + Создание сообщения, пригодного для отправки через TCP + :param dict_msg: dict + :return: str + """ + str_msg = json.dumps(dict_msg) + return str_msg.encode('utf-8') + + +def unpack(bt_str): + """ + Распаквка полученного сообщения + :param bt_str: str + :return: dict + """ + str_decoded = bt_str.decode('utf-8') + return json.loads(str_decoded) \ No newline at end of file diff --git a/hw_Lesson_03/server/settings.py b/hw_Lesson_03/server/settings.py new file mode 100644 index 0000000..0107932 --- /dev/null +++ b/hw_Lesson_03/server/settings.py @@ -0,0 +1,3 @@ +HOST = "127.0.0.1" +PORT = 7777 +BUFFERSIZE = 1024